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}

* ** *** **** ***** ****** *******Use a prompt box to enter the number of rows.
// ExpressionInterpolation Example
var name = "Larry";
var amt = "347.81";
document.writeln(`${name} owes me $${amt}.`);
// Output:
Larry owes me $347.81.
var feet = 13; var inches = 8; var output = feet + "\' " + inches + "\"";
5 == 5, "5" == "5", 5 == "5", 0 == false, 1 == trueall return true.
document.writeln(`${5 == 5} ${5 == 3} ${"5" == "5"}<br>`);
// The string "5" is converted to a number before
// performing the comparison:
document.writeln(`${5 == "5"}`);
// Output:
true false true
true
document.writeln(`${0 == false} ${5 == true} ${0.00000001 == false}`);
// Output:
true false false
document.writeln(`${"1" == true} ${"" == false}<br>`);
document.writeln(`${"0" == false} ${"1" == true} ${"5" == true}<br>`);
document.writeln(`${"0.00" == false} ${"1.00" == true} ${"5.00" == true}`);
// Output:
true true false
true true false
true true false
document.writeln(`${5 === "5"}<br>`);
document.writeln(`${"" === false} ${"1" === true}<br>`);
document.writeln(`${"0" === false} ${"1" === true}<br>`);
document.writeln(`${"0.00" === false} ${"1.00" === true}`);
// Output
false
false false
false false
false false
var n = 1.0 - 0.8; var m = 0.3 - 0.1; document.writeln((n == m) + " "); // Output: falseWhy? Because of differing roundoff errors due to the binary representation of the double precision floating point numbers, 1.0 - 0.8 and 0.3 - 0.1 are not represented with exactly the same values. To see this, look at the output of this statement:
document.writeln(n - m); // Output: -2.7755575615628914e-17Instead of checking for equality with floating point values, we should check that n and m are very close to each other:
document.writeln((Math.abs(n - m) < 1.0e15) + " "); // Output: true
var a = [2, 3, 5, 7, 11, 13]; document.writeln(a + "<br>"); document.writeln(a[4]); // Output: 2,3,5,7,11,13 11
a0 = 2; a1 = 3; a2 = 5; a3 = 7; a4 = 11; a5 = 13;
for(var i = 0; i < 5; i++) {
document.write(a[i] + " ");
}
// Output:
2 3 5 7 11 13
or
for(var n of a)) {
document.write(n + " ");
}
// Output:
2 3 5 7 11 13
var a = [45.3, 'apple', false, null];
var a = [2, 3, 5, 7]; document.writeln(typeof a); // Output: object // Reason: an array is not a primitive datatype, it // is a composite datatype called an object type.
document.writeln(typeof [2, 3, 5, 7]);an array is an object.
height vertical jump weight age yearsInPros name jerseyNumber team positionBasketball Player Methods that were suggested in class:
shoot pass dribble rebound run block screen stop
var a = [2, 3, 5, 7];the properties and methods of a are accessed by the dot operator (.).
document.writeln(a.length); // Output: 4When accessing a property from an object or class, parentheses are never used after the property name.
document.writeln(a.reverse( )); // Output: [7, 5, 3, 2]When accessing a function from an object or class, parentheses are always used after the function name. If there are no parameters to pass to the function, empty parentheses are still needed. Here is a method call that uses two parameters:
document.writeln(Math.min(7, 4)); // Output: 4
toString concat pop push shift unshiftAnswer: Check out this W3Schools reference to array methods:
var a = [2, 3, 5, 7]; var b = [9, 8, 7, 6]; // When printing the array object a, toString // is automatically called document.writeln(a.toString( ) + " " + a + "<br>"); // Output: 2,3,5,7 2,3,5,7 // The concat method concatenates two arrays. document.writeln(a.concat(b) + "<br>"); // Output: 2,3,5,7,9,8,7,6 // The pop method pops the element off // of the top (right) of the array. a.pop( ); document.writeln(a + "<br>"); // Output 2,3,5 // The push method pushes a new element // (in this case 99) onto the top of the array. a.push(99); document.writeln(a + "<br>"); // Output: 2,3,5,99 // The shift method is similar to pop, but // but the bottom (left) element is removed. a.shift( ); document.writeln(a + "<br>"); // Output: 3,5,99 // The unshift method is like push, but the new element // is added to the bottom (left) of the array. a.unshift(98); document.writeln(a + "<br>"); // Output: 98,3,5,99 var c = [2, 3, 5, 7, 11, 13, 17, 19]; // The splice method "splices" new elements // into the middle of an array. In the following line, // the 3 means delete items, starting at index 3 // the 4 means delete 4 items // the 8, 7, and 5 are the items to be inserted in the // array to replace the deleted items. c.splice(3, 4, 9, 8, 7, 6); document.writeln(c + "<br>"); // Output: 2,3,4,9,8,7,6,19
at indexOf repeat toUpperCase trimUse the W3Schools String reference to help you:
document.write(Boolean(5) + " ");
document.write(Boolean(283764) + " ");
document.write(Boolean(0) + " ");
document.write(Boolean(-203) + "<br>");
document.write(Boolean(3.14159265) + " ");
document.write(Boolean(-284.378) + " ");
document.write(Boolean(0.000000001) + " ");
document.write(Boolean(0.0) + "<br>");
document.write(Boolean("grapefruit") + " ");
document.write(Boolean("$%&*@") + " ");
document.write(Boolean("0") + " ");
document.write(Boolean("false") + " ");
document.write(Boolean(" ") + " ");
document.write(Boolean("") + "<br>");
document.write(Boolean(null) + " ");
document.write(Boolean(undefined) + " ");
document.write(Boolean(Infinity) + " ");
document.write(Boolean(NaN) + "<br>");
Then run it to check if your prediction is correct.
var n = 45;
var p;
if (n) {
v = "nonzero";
}
else {
v = "zero";
}
document.writeln("Value: " + v);
function makeGreeting(name) {
var greeting = `Hello, ${name}, how are you?`;
return greeting
}
document.writeln(greeting);
The variable greeting does not exist after the end of
the function.
{
let x = 1.5;
x++;
}
document.writeln(x);
These statements generate an error because the variable x does not exist after the block in
which it is defined ends.
// Compute the sum of numbers between
// 1 and n inclusive:
function sum1ToN(n) {
var sum = 0;
for(let i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
document.writeln("Sum: " + sum1ToN(100));
// Output:
5050
// Note: a more efficient way to compute
// the sum of the numbers from 1 to 100
// is to use this formula:
// sum = (n * (n + 1)) / 2
= (100 * 101) / 2
= 10100 / 2
= 5050
Here is a story about the famous mathematician Karl Gauss. When he was eight
years old in elementary school,
the teacher asked the class asked the students to add up the numbers from 1 to 100. Although this would
be a difficult problem for most elementary students, the teacher was surprized
that Karl obtained the answer so quickly. He noticed that
sum = 1 + 2 + 3 + ... + 98 + 99 + 100
= (1 + 100) + (2 + 99) + (3 + 98) + ... + (50 + 51)
= 101 * 50
= 5050
and announced the answer of 5,050 in less than one minute.