Conditionals & Loops
const name = prompt('What is your name?');
const username = 'maxpower';
// If
if (name === 'max' || name === 'peter') {
console.log('Hello');
} else if (name === 'holly') {
console.log('this is holly');
} else {
console.log('not max');
}
// For
const dogsList = document.querySelector('.dogs-list');
const dogs = [
{ name: 'bruce', type: 'chihuahua' },
{ name: 'chance', type: 'bernese' },
];
for (let i = 0; i < dogs.length; i++) {
// Grab a specific dog each time we go through loop
const dog = dogs[i];
// Create a new div for each dog
const dogData = document.createElement('div');
// Add the name and type of our dog to the div
dogData.innerText = `${dog.name} is a ${dog.type}`;
// Append the div to the one we have on the DOM
dogsList.appendChild(dogData);
}
// While
const catsList = document.querySelector('.cats-list');
const cats = [
{ name: 'meow', type: 'long hair' },
{ name: 'purr', type: 'short hair' },
];
let i = 0;
while (i < cats.length) {
// Grab a specific cat each time we go through loop
const cat = cats[i];
// Create a new div for each cat
const catData = document.createElement('div');
// Add the name and type of our cat to the div
catData.innerText = `${cat.name} is a ${cat.type}`;
// Append the div to the one we have on the DOM
catsList.appendChild(catData);
i++;
}
// Do While
const birdsList = document.querySelector('.birds-list');
const birds = [
{ name: 'pepe', type: 'ara' },
{ name: 'nero', type: 'crow' },
];
let j = 0;
do {
// Grab a specific bird each time we go through loop
const bird = birds[j];
// Create a new div for each bird
const birdData = document.createElement('div');
// Add the name and type of our bird to the div
birdData.innerText = `${bird.name} is a ${bird.type}`;
// Append the div to the one we have on the DOM
birdsList.appendChild(birdData);
j++;
} while (j < birds.length)
// For Of
let fishList = document.querySelector('.fish-list');
const fishes = [
{ name: 'nemo', type: 'clownfish' },
{ name: 'dori', type: 'some blue fish I dont know' },
];
for (let fish of fishes) {
// Create a new div for each fish
const fishData = document.createElement('div');
// Add the name and type of our fish to the div
fishData.innerText = `${fish.name} is a ${fish.type}`;
// Append the div to the one we have on the DOM
fishList.appendChild(fishData);
}