Home > Software engineering >  What formula calculates what I want?
What formula calculates what I want?

Time:06-17

I have:

var list = [
    [
      'Link index: 0',  'Link index: 1',
      'Link index: 2',  'Link index: 3',
      'Link index: 4',  'Link index: 5',
      'Link index: 6',  'Link index: 7',
      'Link index: 8',  'Link index: 9',
      'Link index: 10', 'Link index: 11',
      'Link index: 12', 'Link index: 13',
      'Link index: 14', 'Link index: 15',
      'Link index: 16', 'Link index: 17',
      'Link index: 18', 'Link index: 19',
      'Link index: 20', 'Link index: 21',
      'Link index: 22', 'Link index: 23',
      'Link index: 24', 'Link index: 25',
      'Link index: 26', 'Link index: 27',
      'Link index: 28', 'Link index: 29'
    ],
    [
      'Link index: 30', 'Link index: 31',
      'Link index: 32', 'Link index: 33',
      'Link index: 34', 'Link index: 35',
      'Link index: 36', 'Link index: 37',
      'Link index: 38', 'Link index: 39',
      'Link index: 40', 'Link index: 41',
      'Link index: 42', 'Link index: 43',
      'Link index: 44', 'Link index: 45',
      'Link index: 46', 'Link index: 47',
      'Link index: 48', 'Link index: 49',
      'Link index: 50', 'Link index: 51',
      'Link index: 52', 'Link index: 53',
      'Link index: 54', 'Link index: 55',
      'Link index: 56', 'Link index: 57',
      'Link index: 58', 'Link index: 59'
    ],
    [
      'Link index: 60', 'Link index: 61',
      'Link index: 62', 'Link index: 63',
      'Link index: 64', 'Link index: 65',
      'Link index: 66', 'Link index: 67',
      'Link index: 68', 'Link index: 69',
      'Link index: 70', 'Link index: 71',
      'Link index: 72', 'Link index: 73',
      'Link index: 74', 'Link index: 75',
      'Link index: 76', 'Link index: 77',
      'Link index: 78', 'Link index: 79',
      'Link index: 80', 'Link index: 81',
      'Link index: 82', 'Link index: 83',
      'Link index: 84', 'Link index: 85',
      'Link index: 86', 'Link index: 87',
      'Link index: 88', 'Link index: 89'
    ],
    [
      'Link index: 90',  'Link index: 91',
      'Link index: 92',  'Link index: 93',
      'Link index: 94',  'Link index: 95',
      'Link index: 96',  'Link index: 97',
      'Link index: 98',  'Link index: 99',
      'Link index: 100', 'Link index: 101',
      'Link index: 102', 'Link index: 103',
      'Link index: 104', 'Link index: 105',
      'Link index: 106', 'Link index: 107',
      'Link index: 108', 'Link index: 109',
      'Link index: 110', 'Link index: 111',
      'Link index: 112', 'Link index: 113',
      'Link index: 114', 'Link index: 115',
      'Link index: 116', 'Link index: 117',
      'Link index: 118', 'Link index: 119'
    ]
];

for (var chapter = 0; chapter < 1000; chapter  ) {
    var page = '';// Some kind of formula
    var chapter_on_page = '';// Some kind of formula
}

I want a formula that calculates the results for the variable 'page' and the variable 'chapter_on_page'. I tried using this formula for the variable 'page':

var page = Math.ceil(chapter / 30);

And everything works almost perfect, any other ideas?

For the variable 'chapter_on_page' I want this:

return list[page][chapter_on_page];

Requirements are:

  • The calculated 'chapter_on_page' must be a number from 0 - 29 (Belonging to an element in the array)

CodePudding user response:

The page index is simply the whole-number of times the length of the it (30) goes into the counter:

var page = Math.floor(chapter/30);

The chapter part is the remainder of that division (how many are 'left over'). This uses the remainder operator % (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder)

var chapter_on_page = (chapter0);

This working snippet prints out the indexes and the value of the array for that index:

var list = [
    [
      'Link index: 0',  'Link index: 1',
      'Link index: 2',  'Link index: 3',
      'Link index: 4',  'Link index: 5',
      'Link index: 6',  'Link index: 7',
      'Link index: 8',  'Link index: 9',
      'Link index: 10', 'Link index: 11',
      'Link index: 12', 'Link index: 13',
      'Link index: 14', 'Link index: 15',
      'Link index: 16', 'Link index: 17',
      'Link index: 18', 'Link index: 19',
      'Link index: 20', 'Link index: 21',
      'Link index: 22', 'Link index: 23',
      'Link index: 24', 'Link index: 25',
      'Link index: 26', 'Link index: 27',
      'Link index: 28', 'Link index: 29'
    ],
    [
      'Link index: 30', 'Link index: 31',
      'Link index: 32', 'Link index: 33',
      'Link index: 34', 'Link index: 35',
      'Link index: 36', 'Link index: 37',
      'Link index: 38', 'Link index: 39',
      'Link index: 40', 'Link index: 41',
      'Link index: 42', 'Link index: 43',
      'Link index: 44', 'Link index: 45',
      'Link index: 46', 'Link index: 47',
      'Link index: 48', 'Link index: 49',
      'Link index: 50', 'Link index: 51',
      'Link index: 52', 'Link index: 53',
      'Link index: 54', 'Link index: 55',
      'Link index: 56', 'Link index: 57',
      'Link index: 58', 'Link index: 59'
    ],
    [
      'Link index: 60', 'Link index: 61',
      'Link index: 62', 'Link index: 63',
      'Link index: 64', 'Link index: 65',
      'Link index: 66', 'Link index: 67',
      'Link index: 68', 'Link index: 69',
      'Link index: 70', 'Link index: 71',
      'Link index: 72', 'Link index: 73',
      'Link index: 74', 'Link index: 75',
      'Link index: 76', 'Link index: 77',
      'Link index: 78', 'Link index: 79',
      'Link index: 80', 'Link index: 81',
      'Link index: 82', 'Link index: 83',
      'Link index: 84', 'Link index: 85',
      'Link index: 86', 'Link index: 87',
      'Link index: 88', 'Link index: 89'
    ],
    [
      'Link index: 90',  'Link index: 91',
      'Link index: 92',  'Link index: 93',
      'Link index: 94',  'Link index: 95',
      'Link index: 96',  'Link index: 97',
      'Link index: 98',  'Link index: 99',
      'Link index: 100', 'Link index: 101',
      'Link index: 102', 'Link index: 103',
      'Link index: 104', 'Link index: 105',
      'Link index: 106', 'Link index: 107',
      'Link index: 108', 'Link index: 109',
      'Link index: 110', 'Link index: 111',
      'Link index: 112', 'Link index: 113',
      'Link index: 114', 'Link index: 115',
      'Link index: 116', 'Link index: 117',
      'Link index: 118', 'Link index: 119'
    ]
];

let report = "";

for (var chapter = 0; chapter < 120; chapter  ) {
    var page = Math.floor(chapter/30);
    var chapter_on_page = (chapter0);
    report  = `${page}, ${chapter_on_page}, '${list[page][chapter_on_page]}'<br>`;
}

document.body.innerHTML = report;

Incidentally, you could just as easily construct the strings according to the chapter counter, like this:

let report = "";

for (var chapter = 0; chapter < 120; chapter  ) {
    report  = `Link index: ${chapter}<br>`;
}

document.body.innerHTML = report;

  • Related