Merge branch 'main' of https://github.com/PacktPublishing/JavaScript-from-Beginner-to-Professional into main
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
let counter = 0;
|
||||
let step = 5;
|
||||
do {
|
||||
console.log(counter);
|
||||
counter += step;
|
||||
}
|
||||
while (counter <= 100);
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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]);
|
||||
}
|
||||
@@ -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);
|
||||
@@ -0,0 +1,11 @@
|
||||
const myTable = [];
|
||||
const numm = 10;
|
||||
for(let x=0; x<numm; x++){
|
||||
const temp = [];
|
||||
for(let y = 0; y<numm; y++){
|
||||
temp.push(x*y);
|
||||
}
|
||||
myTable.push(temp);
|
||||
}
|
||||
|
||||
console.table(myTable);
|
||||
Reference in New Issue
Block a user