1

I have an assignment for a Javascript course where I have to count how many of each specific types of elements occur in an array. The array is 105 elements long, and just occurrences of the numbers 1, 2, 3, 4, and 5. I have to count how many 1's, 2's, 3's, etc.

Of course there's a simple way to do this using a loop however my teacher has added the following at the end of the assignment:

Use only the length property, toString(), sort() and indexOf() methods. Please no loops or conditional statements.

I have no idea how to do this assignment without using a loop. Any help you can give me is greatly appreciated. Thanks!

2
  • After sorting the array, look at the indexes of first occurrences of digits. Commented Sep 29, 2013 at 13:45
  • ok then what do I do? array sorted. how do I fetch the total count without a loop or statement? Commented Feb 10, 2015 at 4:40

3 Answers 3

2

this will be the answere:

var c=[1,2,3,4,5,2,3,4,5]
    c.sort()

    cout_of_1 = c.indexOf(2)-c.indexOf(1);
    cout_of_2 = c.indexOf(3)-c.indexOf(2);
    cout_of_3 = c.indexOf(4)-c.indexOf(3);
    cout_of_4 = c.indexOf(5)-c.indexOf(4);
    cout_of_5 = c.length-c.indexOf(5)+1;
Sign up to request clarification or add additional context in comments.

Comments

1

Since this is an assignment and you haven't really shown an attempt, I'll just give you a couple of hints.

  • Once you sort the array, all of the 1s will be grouped together at the start of the array, then all the 2s, etc.
  • The indexOf function returns the first index at which a given element can be found in the array, or -1 if it is not present.

Given the index of the first occurrence of each digit in a sorted array, you should be able to calculate how many of each digit there are. I'm not sure you'll need toString() at all. Your instructor might have thrown that in to be sneaky.

1 Comment

I still dont understand how to get the total count of a number after I sorted the array. totally lost. all the solutions around are with loops or statements. Isn't this suppose to be as easy as introductory course exercises so you can understand as array? No wonder why you get discouraged so easily.
0

First, sort the array, then grab the first occurrence of the next value. That's the amount of numbers you have, minus the ones you already have.

var array = [1,2,5,3,2,4,5,2,1,3,3,4,5,6,2];
array = array.sort();

var ones = array.indexOf(2);
var twos = array.indexOf(3) - ones;
var threes = array.indexOf(4) - ones - twos;
...

However for the last value (5) there is no next value to check. You can work around this by checking the length and subtracting one from it.

var fives = array.length - ones - twos - threes - fours;

Keep in mind that this is actually bad code, and you should use loops.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.