diff --git a/Chapter 5/Exercise 5.1 b/Chapter 5/Exercise 5.1 new file mode 100644 index 0000000..db89ec2 --- /dev/null +++ b/Chapter 5/Exercise 5.1 @@ -0,0 +1,16 @@ +const max = 5; +const ranNumber = Math.floor(Math.random() * max) + 1; +//console.log(ranNumber); +let correct = false; +while (!correct) { + let guess = prompt("Guess a Number 1 - " + max); + guess = Number(guess); + if (guess === ranNumber) { + correct = true; + console.log("You got it " + ranNumber) + } else if (guess > ranNumber) { + console.log("Too high"); + } else { + console.log("Too Low"); + } +} diff --git a/Chapter 5/Exercise 5.2 b/Chapter 5/Exercise 5.2 new file mode 100644 index 0000000..6669612 --- /dev/null +++ b/Chapter 5/Exercise 5.2 @@ -0,0 +1,7 @@ +let counter = 0; +let step = 5; +do { + console.log(counter); + counter += step; +} +while (counter <= 100); diff --git a/Chapter 5/Exercise 5.3 b/Chapter 5/Exercise 5.3 new file mode 100644 index 0000000..0d654f5 --- /dev/null +++ b/Chapter 5/Exercise 5.3 @@ -0,0 +1,9 @@ +const myWork = []; +for (let x = 1; x < 10; x++) { + let stat = x % 2 ? true : false; + let temp = { + name: `Lesson ${x}`, status: stat + }; + myWork.push(temp); +} +console.log(myWork); diff --git a/Chapter 5/Exercise 5.4 b/Chapter 5/Exercise 5.4 new file mode 100644 index 0000000..766ec7e --- /dev/null +++ b/Chapter 5/Exercise 5.4 @@ -0,0 +1,13 @@ +const myTable = []; +const rows = 4; +const cols = 7; +let counter = 0; +for (let y = 0; y < rows; y++) { + let tempTable = []; + for (let x = 0; x < cols; x++) { + counter++; + tempTable.push(counter); + } + myTable.push(tempTable); +} +console.table(myTable); diff --git a/Chapter 5/Exercise 5.5 b/Chapter 5/Exercise 5.5 new file mode 100644 index 0000000..0e46e7d --- /dev/null +++ b/Chapter 5/Exercise 5.5 @@ -0,0 +1,17 @@ +const grid = []; +const cells = 64; +let counter = 0; +let row; +for (let x = 0; x < cells + 1; x++) { + if (counter % 8 == 0) { + if (row != undefined) { + grid.push(row); + } + row = []; + } + counter++; + let temp = counter; + row.push(temp); + +} +console.table(grid); diff --git a/Chapter 5/Exercise 5.6 b/Chapter 5/Exercise 5.6 new file mode 100644 index 0000000..9d23cec --- /dev/null +++ b/Chapter 5/Exercise 5.6 @@ -0,0 +1,12 @@ +const myArray = []; +for (let x = 0; x < 10; x++) { + myArray.push(x + 1); +} +console.log(myArray); + +for (let i = 0; i < myArray.length; i++) { + console.log(myArray[i]); +} +for (let val of myArray) { + console.log(val); +} diff --git a/Chapter 5/Exercise 5.7 b/Chapter 5/Exercise 5.7 new file mode 100644 index 0000000..4d306ce --- /dev/null +++ b/Chapter 5/Exercise 5.7 @@ -0,0 +1,16 @@ +const obj = { + a: 1, + b: 2, + c: 3 +}; +for (let prop in obj) { + console.log(prop, obj[prop]); +} +const arr = ["a", "b", "c"]; +for (let w = 0; w < arr.length; w++) { + console.log(w, arr[w]); +} + +for (el in arr) { + console.log(el, arr[el]); +} diff --git a/Chapter 5/Exercise 5.8 b/Chapter 5/Exercise 5.8 new file mode 100644 index 0000000..977780b --- /dev/null +++ b/Chapter 5/Exercise 5.8 @@ -0,0 +1,23 @@ +let output = ""; +let skipThis = 7; +for (let i = 0; i < 10; i++) { + if (i === skipThis) { + continue; + } + output += i; +} +console.log(output); + + +//Alternatively, the following code could be used, replacing continue with break: + +let output = ""; +let skipThis = 7; +for (let i = 0; i < 10; i++) { + if (i === skipThis) { + break; + } + output += i; +} + +console.log(output); diff --git a/Chapter 5/Project 1 b/Chapter 5/Project 1 new file mode 100644 index 0000000..8c2cba3 --- /dev/null +++ b/Chapter 5/Project 1 @@ -0,0 +1,11 @@ +const myTable = []; +const numm = 10; +for(let x=0; x