var amount = 23.5425643; var amtRounded = Math.floor(amount * 1000) + 1; // Details of calculation // Original amount: 23.5425643; // Amount after multiplying by 1000: 23542.5643 // Amount after taking the floor: 23542 // Amount after dividing by 1000: 235.42
var randomValue = Math.floor(Math.random( ) * 6) + 1
// JS Expression Random Output Range
// Math.random( ) [0, 1)
// Math.random( ) * 6 [0, 6)
// Math.floor(Math.random( ) * 6) {0, 1, 2, 3, 4, 5}
// Math.floor(Math.random( ) * 6) + 1 {1, 2, 3, 4, 5, 6}

// Display the numbers from 1 to 20:
for(var n = 1; n <= 20; i++) {
document.writeln(n + " ");
}
// Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Corresponding while loop, which still has the iteration,
// condition, and iteration statements, but not as conveniently
// displayed on the same line:
var n = 1;
while(n <= 20) {
document.writeln(n + " ");
n++;
}
// Display the odd numbers less than 20:
for(var n = 1; n < 20; i += 2) {
document.writeln(n + " ");
}
// Output: 1 3 5 7 9 11 13 15 17 19
// Corresponding while loop:
var n = 1;
while(n <= 20) {
document.writeln(n + " ");
n += 2;
}
// Start with 1 and keep doubling
// the number while the number is
// less than 100.
for(var n = 1; n < 100; i *= 2) {
document.writeln(n + " ");
}
// Output: 1 2 4 8 16 32 64
// Corresponding while loop:
var n = 1;
while(n <= 20) {
document.writeln(n + " ");
n *= 2;
}
* ** *** **** ***** ****** *******Use a prompt box to enter the number of rows. Answer:
var numRows = parseInt(prompt("Enter number of rows", "0"));
for(var i = 1; i <= numRows; i++) {
for(var j = 1; j <= i; j++) {
document.write("*");
}
document.writeln("<br>");
}