0

Hi there,

I've been given a task:

ARRAY TASK

And here is what I have tried so far:

var myArray = [1, 5, 1, 1, 5, 2, 4, 5, 2, 3, 2, 4, 2, 4, 2, 3, 2, 3, 2, 4, 2, 3, 2, 4, 2, 5, 1, 4, 2, 4, 2, 3, 2, 4, 1, 1, 5, 1, 1, 5, 2, 4, 5, 2, 3, 2, 4, 2, 4, 2, 3, 2, 3, 2, 4, 2, 3, 2, 4, 2, 5, 1, 4, 2, 4, 2, 3, 2, 4, 1, 1, 5, 1, 1, 5, 2, 4, 5, 2, 3, 2, 4, 2, 4, 2, 3, 2, 3, 2, 4, 2, 3, 2, 4, 2, 5, 1, 4, 2, 4, 2, 3, 2, 4, 1];


document.writeln("myArray elements<br/>");
document.writeln(myArray + "<br/>");
document.writeln("myArray has " + myArray.length + " elements" + "<br/>");
document.writeln("My Array sorted: <br/>");
myArray.sort(function(a, b) {
  return a - b
});;
document.writeln(myArray + "<br/>");
document.writeln("First Occurrence of number " + "?" + " in myArray Sorted is at index: " + myArray.indexOf(1) + "<br/>");
document.writeln("First Occurrence of number " + "?" + " in myArray Sorted is at index: " + myArray.indexOf(2) + "<br/>");
document.writeln("First Occurrence of number " + "?" + " in myArray Sorted is at index: " + myArray.indexOf(3) + "<br/>");
document.writeln("First Occurrence of number " + "?" + " in myArray Sorted is at index: " + myArray.indexOf(4) + "<br/>");
document.writeln("First Occurrence of number " + "?" + " in myArray Sorted is at index: " + myArray.indexOf(5) + "<br/>");
document.writeln("myArray contains " + "?" + " occurrence of number " + "?" + "<br/>");
document.writeln("myArray contains " + "?" + " occurrence of number " + "?" + "<br/>");
document.writeln("myArray contains " + "?" + " occurrence of number " + "?" + "<br/>");
document.writeln("myArray contains " + "?" + " occurrence of number " + "?" + "<br/>");
document.writeln("myArray contains " + "?" + " occurrence of number " + "?" + "<br/>");
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Task 2 with Arrays</title>
</head>

<body>
  <h1>Task 2 with Arrays</h1>



</body>

</html>

Where the: ? mark is where I've stuck.

I can only use the:

  • length property,
  • toString()
  • sort()
  • indexOf()

Please note no loops, conditional statements allowed.

Please, does anyone can help me with that?

Thanks

4
  • is lastIndexOf allowed? Commented Feb 13, 2018 at 20:45
  • 1
    If you can't use lastIndexOf, you could sort the array in reverse order and then use indexOf. Then do a little bit of math with the indexes and the length to find out how many of a specific number is in the array. Commented Feb 13, 2018 at 20:49
  • It is not clear from the question what can be hard-coded. Is the 1 in the phrase "First Occurrence of number 1 in myArray Sorted is at index:" part of the fixed text? Is the fact that there are exactly five such phrases predefined? I suppose so, as with the restrictions it is impossible to make that dynamic. Commented Feb 13, 2018 at 20:54
  • I don't think so that I'm allowed to use a "lastIndexOf". Because from the instruction as you can see that it saying we have to ONLY use: length property, toString() sort() indexOf() Commented Feb 13, 2018 at 21:31

3 Answers 3

1

var myArray = [1, 5, 1, 1, 5, 2, 4, 5, 2, 3, 2, 4, 2, 4, 2, 3, 2, 3, 2, 4, 2, 3, 2, 4, 2, 5, 1, 4, 2, 4, 2, 3, 2, 4, 1, 1, 5, 1, 1, 5, 2, 4, 5, 2, 3, 2, 4, 2, 4, 2, 3, 2, 3, 2, 4, 2, 3, 2, 4, 2, 5, 1, 4, 2, 4, 2, 3, 2, 4, 1, 1, 5, 1, 1, 5, 2, 4, 5, 2, 3, 2, 4, 2, 4, 2, 3, 2, 3, 2, 4, 2, 3, 2, 4, 2, 5, 1, 4, 2, 4, 2, 3, 2, 4, 1];


document.writeln("myArray elements<br/>");
document.writeln(myArray + "<br/>");
document.writeln("myArray has " + myArray.length + " elements" + "<br/>");
document.writeln("My Array sorted: <br/>");
myArray.sort(function(a, b) {
  return a - b
});;
document.writeln(myArray + "<br/>");
document.writeln("First Occurrence of number " + "1" + " in myArray Sorted is at index: " + myArray.indexOf(1) + "<br/>");
document.writeln("First Occurrence of number " + "2" + " in myArray Sorted is at index: " + myArray.indexOf(2) + "<br/>");
document.writeln("First Occurrence of number " + "3" + " in myArray Sorted is at index: " + myArray.indexOf(3) + "<br/>");
document.writeln("First Occurrence of number " + "4" + " in myArray Sorted is at index: " + myArray.indexOf(4) + "<br/>");
document.writeln("First Occurrence of number " + "5" + " in myArray Sorted is at index: " + myArray.indexOf(5) + "<br/>");
document.writeln("myArray contains " + (myArray.indexOf(2)-myArray.indexOf(1)) + " occurrence of number " + 1 + "<br/>");
document.writeln("myArray contains " + (myArray.indexOf(3)-myArray.indexOf(2)) + " occurrence of number " + 2 + "<br/>");
document.writeln("myArray contains " + (myArray.indexOf(4)-myArray.indexOf(3)) + " occurrence of number " + 3 + "<br/>");
document.writeln("myArray contains " + (myArray.indexOf(5)-myArray.indexOf(4)) + " occurrence of number " + 4 + "<br/>");
document.writeln("myArray contains " + (myArray.length-myArray.indexOf(5)) + " occurrence of number " + 5 + "<br/>");
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Task 2 with Arrays</title>
</head>

<body>
  <h1>Task 2 with Arrays</h1>



</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

0

You have the first occurrences of all the numbers in the sorted array. So you can do simple subtraction to get the difference between the first occurrence index for each two adjacent numbers.

1 Comment

The subtraction could be as an Option but as you can see it's not clear if I can do like that it's just saying that I can't use loops and conditional statements. But I will give a try as you said it might work, but now sure if it's going to be counted as the Right Answer from their point of view =)
0

Tip: If the array is correctly sorted the first occurrence of 2 is counting all 1.

myArray.indexOf(2) - myArray.indexOf(1)

is counting all occurrences of 1.

Just do some simple math to find where and how many there are of the other numbers.

With the tools you're given, I don't think there's a way to dynamically find all unique numbers in the array, you'll need to hardcode them.

1 Comment

Your right that the problem as I'm not allowed to use any loops or conditional statement. But the task should be completed as from the picture provided above.I really appreciate your help and time. Thanks.

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.