for(var n = 1; n <= 1000; n++) {
document.writeln(n + " ");
if (n % 10 == 0) {
document.writeln("<br>");
}
}
for(var i = 1; i <= 100; i++) {
for(var j = 1; j <= 200; j++) {
for(var k = 1; k <= 300; k++) {
document.writeln("*<br>");
}
}
}
Answer: 100 * 200 * 300 = 6,000,000 (6 million).
for(var i = 1; i <= 100; i++) {
document.writeln("*<br>");
}
for(var j = 1; j <= 200; j++) {
document.writeln("*<br>");
}
for(var k = 1; k <= 300; k++) {
document.writeln("*<br>");
}
Answer: 100 + 200 + 300 = 6,000 (6 thousand).1 2 3 Fizz 4 5 Buzz 6 Fizz 7 8 9 Fizz 10 Buzz 11 12 Fizz 13 15 Fizz Buzz 16 17 ...Answer:
for(var i = 1; i <= 100; i++) {
document.write(i);
if (i % 3 == 0) {
document.write(" Fizz");
}
if (i % 5 == 0) {
document.write(" Buzz");
}
document.writeln("<br>");
}
someObject.someMethod( ); someClass.someMethod( ); someFunction( );Technical Answer: A function that is actually called from the global object window like this:
window.someFunction( );When the global object window is omitted, it is understood, so someFunction( ) and window.someFunction( ) have the same effect.
isFinite isNaN parseFloat parseIntWe can use this script to test them:
document.writeln(isFinite(34544.5484));
// Output: true
document.writeln(window.isFinite(34544.5484));
// Output: true
document.writeln(Number.MAX_VALUE);
// Output: 1.7976931348623157e+308
document.writeln(isFinite(1.0e309) + " ");
// Output: false
document.writeln(isNaN(0 / 0));
// Output: true
document.writeln(parseFloat("453.45"));
// Output: 453.45
document.writeln(parseInt("453");
// Output: 453
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 < 6; 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 weight college position isRookie name team jerseyNumberBasketball Player Methods that were suggested in class:
dribble shoot pass dunk run foul stop block
var a = [2, 3, 5, 7];the properties and methods of a are accessed by the dot operator (.).
document.writeln(a.length); // Output: 4
document.writeln(a.reverse( )); // Output: [7, 5, 3, 2]
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
indexOf repeat toUpperCase trimUse the W3Schools String reference to help you:
var a = "elephant";
document.writeln(a.indexOf("ph");
// Output: 3
document.writeln(a.repeat(4));
// Output: elephantelephantelephantelephant
document.writeln(a.toUpperCase( ));
// Output: ELEPHANT
var b = " Sample Text ";
document.writeln("*" + a.trim( ) + "*");
// Output: *Sample Text*
var obj = {
p1: v1,
p2: v2,
p3: v3
};
var v = obj.p;
var b = {
name: "Michael Jordan",
jerseyNumber: 23,
ht: 1.98,
wt: 98,
team: "Bulls"
};
var k = {
name: "Alice",
gender: "F",
age: 11
};
document.writeln(`${k.name} ${k.gender} ${k.age}`);
// Output: Alice F 11
var k = { };
k.name = "Alice";
k.gender = "F";
k.age = 11;
document.writeln(`${k.name} ${k.gender} ${k.age}`);
// Output: Alice F 11
var k = {
name: "Alice",
gender: "F",
age: 11,
haveBirthday: function( ) {
this.age++;
},
toString: function( ) {
return `${this.name};${this.gender};${this.age}`;
}
};
document.writeln(`${k.toString( )}<br>`);
document.writeln(`${k}<br>`);
k.haveBirthday( );
document.writeln(`${k.age}`);
// Output:
Alice;F;11
Alice;F;11
12
var pair = {
x: 3,
y: 5,
toString: function( ) {
return `(${this.x}, ${this.y})`;
}
}
document.writeln(pair.x + " " + pair.y + " ");
// or
// document.writeln(`${pair.x} ${pair.y}`);
document.writeln(pair);
// Define p as an array of object literals:
p = [ {name: "Alice", gender: "F", age: 11},
{name: "Bob", gender: "M", age: 10},
{name: "Chloe", gender: "F", age: 8} ];
// Convert array to a JSON string:
s = JSON.stringify(p);
document.writeln(s);
// Output:
[{"name":"Alice","gender":"F","age":11},
{"name":"Bob","gender":"M","age":10},
{"name":"Chloe","gender":"F","age":8}]
// Convert JSON string back to an array:
p1 = JSON.parse(s);
for(let i = 0; i <= 2; i++) {
document.writeln(p1[i].name + " ");
document.writeln(p1[i].gender + " ");
document.writeln(p1[i].age + "<br>");
}
// Output:
Alice F 11
Bob M 10
Chloe F 8
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 surprised
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.