Loops Flashcards

1
Q

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.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how can you include a function into a loop? also, how can you avoid the number 2 to repeat itself?

A
for(let i = 0; i < 10; i++){
  if(i === 2){
    console.log(`It's my fav number`);
     continue;
  }
  console.log('Number ' + i);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How you break a FOR loop?

A
for(let i = 0, i < 10, i++){
console.log('Number will stop at 7')
}

if(I === 5){
console.log(‘It stoped on 5’)
break;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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);
}

A

let i = 0;

while(i < 10){
console.log(‘Numbers ‘ + i);
i++;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which loops use the function?

A

MAP and FOREACH

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

const cars = [‘Ford’, ‘Chevy’, ‘Honda’, ‘Toyota’];

for(let i = 0; i < cars.length; i++){
  console.log(cars[i]);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

With what loop can you look through an array besides LOOP TROUGH ARRAY?

A

FOREACH

cars.forEach(function(car){
console.log(car);
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does the index work in the FOREACH array?

A

cars.forEach(function(car, index){
console.log(${index} : ${car});
console.log(array);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is map used for? and how it looks?

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the FOR IN loop and how it works?

It’s often used for objects

A
const user = {
  firstName: 'Yee',
  age: 52,
  address: 'Youmama'
};

for(let x in user){
console.log(${x} : ${user[x]})
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly