Loops Flashcards
How a loop of FOR it’s formed? (i++ means I = I +1)
for(declare variable(var, let; only); (function that it’s going to execute); (i++)(reason unknown){
}
Do a loop command taking into consideration what it’s been mentioned above.
for(let i = 0; i < 10; i++){
}
in this we are declaring i and assigning a value, the function means that i will go up until is less than 10, this is indicated by i++, which says that it will increment one until what the function asks its meet.
how can you include a function into a loop? also, how can you avoid the number 2 to repeat itself?
for(let i = 0; i < 10; i++){ if(i === 2){ console.log(`It's my fav number`); continue; } console.log('Number ' + i); }
How you break a FOR loop?
for(let i = 0, i < 10, i++){ console.log('Number will stop at 7') }
if(I === 5){
console.log(‘It stoped on 5’)
break;
}
do a while loop taking into consideration the following instructions.
Declare variable;
create while as a function(function here){
what it’s going to log here;
i++(very important);
}
let i = 0;
while(i < 10){
console.log(‘Numbers ‘ + i);
i++;
}
Which loops use the function?
MAP and FOREACH
how does a LOOP TROUGH ARRAY look like?
remember that you have to declare your variable, in this case, a CONST to start your loop.
ANSWER KIND OFF;
after you declare your const as an array(u looping through en array so it’s obvious) you will do a for as normal however you have to instead of doing I < a number in here, you have to loop the array by calling the array.whatever it’s happening.
const cars = [‘Ford’, ‘Chevy’, ‘Honda’, ‘Toyota’];
for(let i = 0; i < cars.length; i++){ console.log(cars[i]); }
With what loop can you look through an array besides LOOP TROUGH ARRAY?
FOREACH
cars.forEach(function(car){
console.log(car);
})
How does the index work in the FOREACH array?
cars.forEach(function(car, index){
console.log(${index} : ${car}
);
console.log(array);
});
What is map used for? and how it looks?
It’s used to return a different array.
const users = [ {id: 1, name: 'John'}, {id: 2, name: 'Sara'}, {id: 3, name: 'Karen'} ];
const ids = users.map(function(user){ return user.id; });
console.log(ids);
What is the FOR IN loop and how it works?
It’s often used for objects
const user = { firstName: 'Yee', age: 52, address: 'Youmama' };
for(let x in user){
console.log(${x} : ${user[x]}
)
}