a swirl

Loops, Arrays and Objects


Code

for (i=1 ; i<=12 ; i++){
    for (var j=1; j<=12 ; j++){
        console.log(`${j} x ${i} = ${i*j}`);
    }
}
                                

Results

Sample of Results Only
1 x 2 = 2
2 x 2 = 4
3 x 2 = 6
4 x 2 = 8
5 x 2 = 10
6 x 2 = 12
7 x 2 = 14
8 x 2 = 16
9 x 2 = 18
10 x 2 = 20
11 x 2 = 22
12 x 2 = 24
1 x 3 = 3
2 x 3 = 6
3 x 3 = 9
4 x 3 = 12
5 x 3 = 15
6 x 3 = 18
7 x 3 = 21
8 x 3 = 24
9 x 3 = 27
10 x 3 = 30
11 x 3 = 33
12 x 3 = 36
....
....
1 x 12 = 12
2 x 12 = 24
3 x 12 = 36
4 x 12 = 48
5 x 12 = 60
6 x 12 = 72
7 x 12 = 84
8 x 12 = 96
9 x 12 = 108
10 x 12 = 120
11 x 12 = 132
12 x 12 = 144              
                                

Code

var favouriteFoods = ['apple', 'carrot', 'fresh peas', 'salmon', 'leeks'];
console.log(favouriteFoods[0]);
console.log(favouriteFoods[2]);
console.log(favouriteFoods[4]);
                                

Results

apple
fresh peas
leeks              
                                

Code

var favouriteFoods = ['apple', 'carrot', 'fresh peas', 'salmon', 'leeks'];
for (var i=0 ; i<favouriteFoods.length ; i++){
    console.log(favouriteFoods[i]);
}
                                

Results

apple
carrot
fresh peas
salmon
leeks              
                                

Task4: Objects

First the code. The results for this code are right at the end of this page because document.write is used in the code instead of console.log

let myRecipe = {
    title: 'Chicken noodles',
    servings: 2,
    ingredients: ['chicken','onions','noodles','tomatoes'],
    directions: ['Fry the chicken','Add the onions, tomatoes and noodles'],
    letsCook: function() {
        document.write('I am hungry!! Lets cook ' + this.title + '<br>');
    }
}
document.write(myRecipe.title + '<br>');
document.write(`serves ${myRecipe.servings} <br>`);
document.write(myRecipe.ingredients.join(', ') + '<br>');
document.write(myRecipe.directions.join('. ')+'. <br>');
for (i=0 ; i<myRecipe.ingredients.length ; i++){
    document.write(`- ${myRecipe.ingredients[i]}<br>`);
}
for (i=0 ; i<myRecipe.directions.length ; i++){
    document.write(`- ${myRecipe.directions[i]}<br>`);
}
myRecipe.letsCook();

Objects can also hold functions

Code

var alexa = {
    age: 34,
    hairColour: 'black',
    talk: function() {
        console.log('I am listening');
    },
    eat: function(food) {
        console.log(`Yum, I love ${food}`);
    }
};

alexa.talk();
alexa.eat('pizza');
                

Results

I am listening
Yum, I love pizza              
                    

Output of objects code above