top of page
Search

JavaScript Final bits of code

  • Writer: Keshav Batra
    Keshav Batra
  • Aug 11, 2022
  • 3 min read

For now I've decided to take a break from that JavaScript video, and finish off with do while loops. I have learned about nested objects, nested arrays, for loops, do while loops, looking up objects, and while loops. Here are some new lines of code that I have worked on.



Objects for Lookup

function phoneticLookup(val) {
    var result = "";

    var lookup = {
        "alpha": "Adams",
        "bravo": "Boston",
        "charlie": "Chicago",
        "delta": "Denver",
        "echo": "Easy",
        "foxtrot": "Frank"
    };
    result = lookup[val];

    return result; 
}

console.log(phoneticLookup("foxtrot")) //"Frank" will go through

I figured out that since "foxtrot" is next to "Frank", the value will equal Frank


Nested Arrays

var myPlants = [ //A nested array holds an array within an array
    {
        type: "flowers", //This represents myPlants[0]
        list: [ //This property is an array that multiple texts 
            "rose",
            "tulip",
            "dandelion"
        ]
    },
    {
        type: "trees", //This represents myPlants[1]
        list: [
            "fir",
            "pine", //This represents list[1]
            "birch"
        ]
    }
];
var secondTree = myPlants[1].list[1]; //"Pine" will go through the console since it is list[1] and since "trees" is myPlant[1]

I figured that since the second tree listed listed "pine" and since its in the second column which is myPlant[1], "pine" will go through.


While Loops

//While Loops

var myArray = [];

var i = 0;
while(i < 5) { //The loop is going to keep repeating as long as i < 5
    myArray.push(i); //The number is being added to the array depending on the value of the variable 
    i++; 
}

console.log(myArray); //[0,1,2,3,4] will be passed through

I figured out that the loops will repeat as long as i < 5 the loop will keep repeating and i++ will keep adding 1 until it reaches 4.


For Loops

//For Loops

var ourArray = []; 

for (var i = 0; i < 5; i++) { //As long as i < 5 the loop will keep repeating
    ourArray.push(i);
}

var myArray = [];

for (var i = 1; i < 6; i++) { //As long as i < 6 the loop will keep repeating
    myArray.push(i); //The number determined by i is being added to the array
}

console.log(myArray); //[1,2,3,4,5] will be passed through


Odd Numbers With a For Loop

//Odd Numbers With a For Loop

var ourArray = [];

for (var i = 0; i < 10; i += 2) { //The loop will keep repeating as long as i < 10
    ourArray.push(i); //All the even numbers will be added starting from 0 and ending at 8 since its less than 10
}

console.log(ourArray); //[0,2,4,6,8] will be passed through

var myArray = [];

for (var i = 1; i < 10; i += 2){  //The loop will keep repeating as long as i < 10
    myArray.push(i); //All the odd numbers will be added starting from 1 and ending at 9 since its less than 10
}

console.log(myArray); //[1,3,5,7,9] will be passed through


Since i < 10, it will only go until 9 in odd numbers


Nested For Loops

function multiplyAll(arr){
    var product = 1;

    for (var i=0; i < arr.length; i++) { //The length would be equal to 3 and repeat 3 times
        for (var j=0; j < arr[i].length; j++) { 
            product *= arr[i][j]; // the i represents the outer array and the j represents the inner array
        }
    }

    return product;
}

var product = multiplyAll([[1,2],[3,4],[5,6,7]]); //The array multiplies every number in the brackets to equal 5,040

console.log(product); // 5,040 would be the final answer

All i had to do with this was multiply all the number in the brackets. The length was 3 and it repeated 3 times.


Do While Loops

var myArray = [];
var i = 10;

do { // A do while loop always runs once before checking a condition
    myArray.push(i); // 10 is being put into the array
    i++; // 1 is added
} while (i < 5); // if it's true, then the loop will repeat and if its false it will stop 
//11 < 5 is false so it will stop

console.log(i, myArray); //11,[10] since the array has 10 put into it

If the loop is true then it will repeat and if its false it'll stop. In this case, the loop ends since its false and since the array has 10 put into it and 1 is added, the final answe came out to be 11,[10].

 
 
 

Comments


bottom of page