0

I have a variable that shows various combinations of numbers:

var sequence = [
    '01-02-03-04-05-06',
    '01-02-03-04-05-12',
    '01-02-03-04-12-30',
    '05-10-15-20-25-30',
    '02-04-06-08-10-12',
    '03-06-09-12-15-17',
    '04-08-12-16-20-24',
    '05-10-15-20-25-30'
];

This loop I created allows me to check each value of this variable separately:

for (var i=0; i<sequence.length; i++){
    number = sequence[i].split('-');
    for (var j=0; j<number.length; j++){
        number = number[j];
    }
};

From then on I couldn't advance the argument. I don't often work with loops and I get a bit lost. I need help and guidance to achieve get the following results and insert them into assigned inputs:

  1. The number that is most repeated in every sequence.
  2. The two numbers,in sequence or not, that are most repeated in every sequence.
  3. The three numbers, in sequence or not, that are most repeated in every sequence.
  4. The four numbers, in sequence or not, that are most repeated in every sequence.
  5. The five numbers, in sequence or not, that are most repeated in every sequence.
  6. The six numbers, insequence or not, that are most repeated in every sequence.

Example:

  1. 01, 02, 03, 04, 05 (four times each)
  2. 01-02, 01-03, 01-04, 02-03, 02-04, 03-04 (three times each) ...

FIDDLE: http://jsfiddle.net/v8r6gojz/

Cœur
  • 37,241
  • 25
  • 195
  • 267
LGVentura
  • 1,547
  • 1
  • 11
  • 17
  • 1
    You can't really expect to become a competent programmer if you get lost when writing loops. They're fundamental to programming. – Barmar Oct 03 '14 at 21:10
  • Can you give an example showing what numbers you are expecting in all inputs? – Weafs.py Oct 03 '14 at 21:14
  • @chipChocolate.py i did an update with an example, but i dont know if it is clear enough – LGVentura Oct 03 '14 at 21:21

2 Answers2

1

Use an array whose indexes are the numbers, and the contents are the number of times that number is repeated:

var counts = [];
for (var i=0; i<sequence.length; i++){
    number = sequence[i].split('-');
    for (var j=0; j<number.length; j++){
        thisnumber = parseInt(number[j], 10);
        counts[thisNumber] = counts[thisNumber] ? count[thisNumber]+1 : 1;
    }
};

You can then use the counts array to answer your questions.

Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

My suggestion is to use a generic javascript object to track the frequency of numbers... Here is a good post that details how to create a javascript object that acts like a C# Dictionary. And, here is one that details how to count occurrences of an object value in an array. Push the two together, and you should be able to put something together.

Community
  • 1
  • 1
guildsbounty
  • 3,326
  • 3
  • 22
  • 34