let temperature = 75;
let weather = "rainy";
if (temperature < 45) {
if (weather == "sunny") {
let clothes = "warm clothes with a hat or some sunglasses";
} else if (weather == "rainy") {
let clothes = "warm, dry clothes with a rain jacket or rain boots";
} else {
let clothes = "bundle up in warm clothes";
}
} else if (temperature < 65) {
if (weather == "sunny") {
let clothes = "layered clothes with a hat or some sunglasses";
} else if (weather == "rainy") {
let clothes = "layered clothes clothes with a rain jacket or rain boots";
} else {
let clothes = "wear layered clothes to adapt to the chilly and warm parts of the day";
}
} else {
if (weather == "sunny") {
let clothes = "light & breathable clothes with a hat or some sunglasses";
} else if (weather == "rainy") {
let clothes = "light & breathable clothes with a rain jacket or rain boots";
} else {
let clothes = "wear light & breathable clothes because it is hawt outside";
}
}
var breakfasts = [
// each of these arrays are elements of the breakfasts array,
// though they are arrays with elements themselves.
["biscuits", "sausage"],
["pancakes", "syrup"],
["kolache", "jelly"]
];
for (let i = 0; i < breakfasts.length; i++) {
// the element is accessed by accessing the i-th element of the
// breakfasts array, which we know is an array itself, so we
// specify which element of this array we want to access:
// remember that the second element of an array has an index of 1
console.log(breakfasts[i][1]);
}
What about scenarios in which there are multiple elements in a 2D array and we want to traverse through all of them, instead of accessing a single element?
In this instance, we implement nested for loops:
var newBreakfasts = [
["biscuits", "sausage", "OJ", "cheese grits", "grapes"],
["pancakes", "syrup", "strawberries", "apple juice", "ham"],
["kolache", "jelly", "bacon", "mango", "chocolate milk"]
];
Using the same methodology as the forloop on line 38 would only result in the second element within each array being printed.
We want to print each item related to each breakfast, however, so we will implement a nested for loop like so:
for (let i = 0; i < newBreakfasts.length; i++) {
// this line is simply printing "Breakfast Option N: " to the
// console to keep the print statements organized
console.log("Breakfast Option " + (i + 1) + " : ")
// pay special attention to the second for loop's statement 2.
// It says it will loop as long as j is less than the length
// of the array ***located at element i***
for (let j = 0; j < newBreakfasts[i].length; j++) {
console.log(newBreakfasts[i][j]);
}
}
let tens = 9;
let ones;
while (tens >= 0) {
ones = 9;
while (ones >= 0) {
console.log(tens + "" + ones);
// here we ensure to decrement the ones variable so that we
// are not caught in an infinite loop
ones--;
}
// here we ensure to decrement the tens variable so that we
// are not caught in an infinite loop
tens--;
}
do {
let count = 10;
console.log("doin the first thing");
do {
count--;
console.log("doin the second thing within the first thing");
} while ((count + 1) > 2);
} while (count > 0);