JavaScript/Loops/Exercises

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Topic: Loops

for (Part 1)[edit | edit source]

1 What is the result?

"use strict";

for (const i = 3; i <= 6; i++) {
  alert(i);
} //

Numbers from 3 to 5
Numbers from 3 to 6
Runtime error
None of the above

2 What is the result?

"use strict";

for (let i = 10; i >= 0; i--) {
  alert(i);
  i--;
} //

Numbers from 10 to 0
Even Numbers from 10 to 0
Odd Numbers from 10 to 0
Runtime error
None of the above

3 What is the result?

"use strict";

for (let i = 5; i < 10; ) {
  alert(i);
  i = i + 3;
  alert(i);
} //

5, 8
5, 8, 8
5, 8, 11
5, 8, 8, 11
Runtime error
None of the above

4 What is the result?

"use strict";

const myString = "abc";
for (let i = myString.length - 1; i >= 0; i--) {
  alert(myString[i]);
} //

a, b, c
c, b, a
Runtime error
None of the above


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,
Click to see solution
"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)

Click to see solution
"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
Click to see solution
"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
<===================>
Click to see solution
"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.
Click to see solution
"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
Click to see solution
"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);

1 What is the result?

"use strict";

const myArray = [10, 11, 12];

for (const x in myArray) {
  alert(x);
} //

for (const x of myArray) {
  alert(x);
} //

0, 1, 2 followed by 10, 11, 12
10, 11, 12 followed by 0, 1, 2
Runtime error

2 What is the result?

"use strict";

const myArray = [10, 11, 12];

for (const x in myArray) {
  alert(myArray[x]);
} //

0, 1, 2
10, 11, 12
Runtime error


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
Click to see solution
"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));