Ad

Your task:
Instead of writing out the same code over and over, let’s make the computer loop through our array for us. We can do this with for loops.

Some help for beginners:

Since this syntax is a little complicated, let’s break it into 4 parts:

Within the for loop’s parentheses, the start condition is var i = 0, which means the loop will start counting at 0.

The stop condition is i < animals.length, which means the loop will run as long as i is less than the length of the animals array. When i is greater than the length of the animals array, the loop will stop looping.

The iterator is i++. This means that each loop, i will have 1 added to it.

And finally, the code block is inside the { ... }. The block will run each loop, until the loop stops.

Tip of the day:
The secret to loops is that i, the variable we created inside the for loop’s parentheses, is always equal to a number. To be more clear, the first loop, i will equal 0, the second loop, i will equal 1, and the third loop, i will equal 2.

This makes it possible to write animals[0], animals[1], animals[2] programmatically instead of by hand. We can write a for loop, and replace the hard coded number with the variable i, like this: animals[i].

var animals = ["Grizzly Bear", "Sloth", "Sea Lion"];

//your code here
}

Create a function that does four basic mathematical operations.

The function should take three arguments - operation(string/char), value1(number), value2(number).
The function should return result of numbers after applying the chosen operation.

example:
basicOp('+', 4, 8) // Output: 12
basicOp('-', 14, 18) // Output: -4
basicOp('*', 5, 4) // Output: 20
basicOp('/', 49, 7) // Output: 7

Suppose we're adding 1 and -1. That would return 0, which would be a valid result for this operation. But trying to use an invalid operation would also return 0, so now there's no concrete way of knowing if the function executed correctly.

function basicOp(operation, value1, value2) {
    switch (operation) {
        case '+':
            return value1 + value2;
        case '-':
            return value1 - value2;
        case '*':
            return value1 * value2;
        case '/':
            return value1 / value2;
        default:
            return 0;
            //Suppose we're adding 1 and -1.
    }
}