Generate a list of 100 random numbers ( between 1 and 10) and store them in a on
ID: 3546021 • Letter: G
Question
Generate a list of 100 random numbers ( between 1 and 10) and store them in a one dimensional array. Use a for loop to go through the nubers and keep track of the number of times number 5 and number 8 occur. Display the contents of your array and then the number of times number 5 and 8. the output should be:
Show all 100 numbers using document.writeand make sure you put space between each number.
Also display the result: Number 5 occured x times and number 8 occurred x times.(replace x with the value)
if possible can you please list steps with the solution. it would really help me out.
Thanks
Explanation / Answer
<html>
<head>
<script>
document.onload=rand(); //doucment.onload calls the rand() function when the page is loaded
function rand()
{
var randArray=new Array(); //Array declaration
var i=0;
while(i<100)
{
randArray[i]=1+Math.floor(Math.random()*11); //Random values ranging 1-10 are assigned to array
i++;
}
i=0;
while(i<100)
{
document.write(randArray[i]); //Displaying the array values using document.write
document.writeln(" ");
i++;
}
var c8=0; //Initialising counters for counting no. of 8 and 5 in array
var c5=0;
i=0;
while(i<100)
{
if(randArray[i]==8)
{
c8++; //Incrementing counter when no. is encountered during array traversal
}
if(randArray[i]==5)
{
c5++;
}
i++;
}
document.writeln("<br>Number 5 occured "+c5+" times<br>");
document.writeln("Number 8 occured "+c8+" times<br>"); //Displaying the counter values
}
</script>
</head>
<body>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.