JavaScript/Loops/Exercises
Topic: Loops
for (Part 1)
[edit | edit source]
for (Part 2)
[edit | edit source]1. Write a script that uses two nested loops and shows the following strings:
1,
1, 2,
1, 2, 3,
1, 2, 3, 4,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 6, 7,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
"use strict";
// by convention, fundamental constants like PI or such constants that
// determine the behavior of your program are written in uppercase
const START = 1;
const END = 10;
let myString = "";
for (let o = START; o <= END; o++) {
myString = "";
for (let i = 1; i <= o; i++) {
myString += i + ", ";
}
alert(myString);
}
2. Write a script that uses two nested loops and shows the following strings, including the apostrophes:
Next line is: "1, 3, 5, 7, 9,"
Next line is: "1, 3, 5, 7,"
Next line is: "1, 3, 5,"
Next line is: "1, 3,"
Next line is: "1,"
(Bonus challenge: Remove the last comma)
"use strict";
// by convention, fundamental constants like PI or such constants that
// determine the behavior of your program are written in uppercase
const START = 1;
const END = 10;
let myString = "";
for (let o = END; o >= START; o = o - 2) {
myString = 'Next line is: "';
for (let i = 1; i <= o; i = i + 2) {
myString += i + ", ";
}
myString += '"';
alert(myString);
}
3. Write a script that creates a 'quadrat' with edge length 4. It consists of 'x's at one diagonal and dots at the other places. Hint: Create an empty string and - within the loop - append characters to the string. The line break is "\n". Lastly, show the string.
- Use
for
loops. - In a second step, change only one single statement to create an according 6 x 6 quadrat.
x...
.x..
..x.
...x
"use strict";
// by convention, fundamental constants like PI or such constants that
// determine the behavior of your program are written in uppercase
const LENGTH = 4;
let result = "";
for (let i = 0; i < LENGTH; i++) {
for (let j = 0; j < LENGTH; j++) {
if (i === j) {
result = result + "x";
} else {
result = result + ".";
}
}
result = result + "\n";
}
alert(result);
// to create a 6x6 quadrat, just change the variable LENGTH to '6'
4. Write a script that creates an 'arrow' with two heads: "<======>". The length of the arrow is determined by a variable. Changing this variable will result in an arrow of a different size.
<=====>
or
<===================>
"use strict";
// by convention, fundamental constants like PI or such constants that
// determine the behavior of your program are written in uppercase
const LENGTH = 4;
// left head
let result = "<";
for (let i = 0; i < LENGTH; i++) {
result = result + "=";
}
// right head
result = result + ">";
alert(result);
for..in, for..of, entries()
[edit | edit source]1. Write a script that
- creates an Object with some of your personal characteristics
- shows all properties (key and value) within a for..in loop
- shows all properties (key and value) by using the Object.entries() method.
"use strict";
// an example
const myObj = {firstName: "Marilyn", familyName: "Monroe", born: 1953};
for (const key in myObj) { // 'in' delivers the keys
alert('First: ' + key + ' / ' + myObj[key]);
}
// 'entries()' delivers key and value within an array-element
for (const [key, val] of Object.entries(myObj)) {
alert('Second: ' + key + ' / ' + val);
}
2. Write a script that
- creates an Array with all odd numbers smaller than 10
- creates a string with all those numbers plus a delimiter using a for..of loop
- shows the string after the loop
"use strict";
const myArray = [];
for (let x = 1; x < 10; x = x + 2) {
myArray.push(x);
}
let result = "";
for (const value of myArray) {
result += value + '; ';
}
alert(result);
forEach
[edit | edit source]1. Write a script that
- defines an array
const arrIntegers = [3, 8, -3, 0];
- use a
.forEach
construction to fill a second array with the quadratic values (9, 64, ...) of the first array - show the second array
"use strict";
// declare both arrays
const arrIntegers = [3, 8, -3, 0];
const arrQuadrat = []; // empty
// compute the quadrats
arrIntegers.forEach((elem) => arrQuadrat.push(elem * elem));
// show the result
arrQuadrat.forEach(elem => alert(elem));