Hi there,
I've been given a 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
lastIndexOfallowed?lastIndexOf, you could sort the array in reverse order and then useindexOf. Then do a little bit of math with the indexes and thelengthto find out how many of a specific number is in the array.