What are the five components of every successful loop? A commonly used mathemati
ID: 3692179 • Letter: W
Question
What are the five components of every successful loop?
A commonly used mathematical function in JavaScript is the floor function. When given a real number, the floor function simply returns the largest integer that is still no larger than the specified real number. In mathematics, floor is often written as the lower half of square brackets (i.e. and ) surrounding the value that you want to find the floor of. For example, 3.1 = 3, 17 = 17, and 8.999999 = 8. In JavaScript, the Math.floor() function computes this value. For the same examples as above, Math.floor(3.1) returns 3, Math.floor(17) returns 17, and Math.floor(8.999999) returns 8. Using this idea, evaluate:
(a) Math.floor(1.3)
(b) Math.floor(99.99)
(c) Math.floor(0.99999999999999) (d) Math.floor(0.00000000000001) (e) Math.floor(-5.1)
(f) Math.floor(-5)
In JavaScript, the Math.random() function gives a random number between 0 and 1, potentially including 0 but excluding 1. Thus, this function will give a random number in the range [0. . .0.999] . With this in mind, answer the following questions:
(a) What range of values could Math.random()*6 result in?
(b) What range of values could Math.random()*67 result in?
(c) List all of the values that Math.floor(Math.random()*6) could give.
(d) List all of the values that 1+Math.floor(Math.random()*6) could give.
(e) Give an assignment statement that would store a random integer number between 1 and 10 (allowing both 1 and 10 to occur) into the variable quizScore.
What would the following JavaScript code sequence output?
sum+=iter; } iter++;
5. What would the following JavaScript code sequence output?
} iter++;
6. What would the following JavaScript code sequence output?
{
7. What would the following JavaScript code sequence output?
} iter++;
8. Consider the following JavaScript function:
{
{
}
What would each of the following calls add to the HTML body?
(a) mystery(2); (b) mystery(3); (c) mystery(4);
9. Consider the following JavaScript function:
{
{
for (var y=1; y<=x; y++) // note small change from prior problem document.write( (x+y) + " " );
}
What would each of the following calls add to the HTML body?
(a) mystery(2); (b) mystery(3); (c) mystery(4);
Explanation / Answer
Please follow the data correctly :
Logically, every loop must have 4 parts:
• Loop initialization statement(s)
• Loop termination condition
• Loop body (the stuff that we want to repeat)
• Loop modification statement(s)
• Programming languages do not usually force you to include
these parts, so you must be careful to do it yourself. Otherwise
your program will get wrong results.
The floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result.
If the passed argument is an integer, the value will not be rounded.
(a) Math.floor(1.3) - 1
(b) Math.floor(99.99) - 99
(c) Math.floor(0.99999999999999) - 0
(d) Math.floor(0.00000000000001) - 0
(e) Math.floor(-5.1) - -6
(f) Math.floor(-5) - -5
The random() method returns a random number from 0 (inclusive) up to but not including 1 (exclusive).
(a) What range of values could Math.random()*6 result in? - 0 to 6
(b) What range of values could Math.random()*67 result in? - 0 to 67
(c) List all of the values that Math.floor(Math.random()*6) could give. - 2
4
0
5
0
1
3
2
5
1
(d) List all of the values that 1+Math.floor(Math.random()*6) could give. - 5 3 4 5 4 3 3 2 5 4 4 1 6 4 4 2 1
(e) Give an assignment statement that would store a random integer number between 1 and 10 (allowing both 1 and 10 to occur) into the variable quizScore. -
Random r = new Random();
int Low = 10;
int High = 100;
int Result = r.nextInt(High-Low) + Low;
output of JS code :
This produces an infinite loop - never ending loop.
5) JS to check if iteration is even
This produces an infinite loop - never ending loop.
6) sum=0
7) This produces an infinite loop - never ending loop.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.