TIL of the Past: 20-10-03
JavaScript - Advanced Loop
Along with the for, while, do, and forEach loop, the topic in this section was the usage of for of and for in loop.
The idea is generally similar to the one of forEach loop, but the difference of ‘for in’ loop compared to the other two was that ‘for in’ was enumerating, while the other two were iterating. A more thorough explanation is provided by this link, to be honest, I’m still not 100% sure. The thing I do remember is though, that Objects are enumerable and not iterable, while arrays are enumerable and iterable.
//Question #1:
//create a function called biggestNumberInArray() that takes
//an array as a parameter and returns the biggest number.
//biggestNumberInArray([-1,0,3,100, 99, 2, 99]) should return 100;
//Use at least 3 different types of javascript loops to write this:
const array = [-1,0,3,100, 99, 2, 99] // should return 100
const array2 = ['a', 3, 4, 2] // should return 4
const array3 = [] // should return 0
function biggestNumberInArray(arr) {
let compare = 0;
arr.forEach(num => {
if (arr.length !== 0) {
if (typeof num === 'number' && num>compare) {
compare = num;
};
};
});
console.log(compare);
}
function biggestNumberInArray2(arr) {
let compare = 0;
for (num of arr) {
if (arr.length !== 0) {
if (typeof num === 'number' && num>compare) {
compare = num;
};
};
};
console.log(compare);
}
function biggestNumberInArray3(arr) {
let compare = 0;
for (let i = 0; i < arr.length; i++) {
let num = arr[i];
if (arr.length !== 0) {
if (typeof num === 'number' && num>compare) {
compare = num;
};
};
};
console.log(compare);
}
//Question #2:
//Write a function checkBasket() that lets you know if the item is in the basket or not
const amazonBasket = {
glasses: 1,
books: 2,
floss: 100
}
function checkBasket(basket, lookingFor) {
for (item in amazonBasket){
if (item === lookingFor){
return `Yes! ${arguments[1]} is in the basket`
}
}
return `No! ${arguments[1]} is not in the basket`
}
While solving the second question, though the logic of finding the item in the object was rather easy, the tough part was returning the right message.
The first problem that I encountered was that while using the template literate to write the message, basket object, passed through as the parameter, couldn’t be used as a variable(?). So just had to replace it with plain text.
The second problem was with how and where to place the messages so that it only shows once, meaning that if during the loop a match has been found, the function should end with the success message, while in the end, where after the loop has reached the end and no match has been found, a failure message should be returned.
The logic behind the solution to this problem was using return
(I tried break
at first), which ends the whole function there. And outside the for loop, the failure message was placed. When the function reaches this point, it means that the for loop has ended without reaching the return, which would have ended the function there, and that there is no match for the item in the basket, ergo a right place to put the message on.