Arrays Flashcards

1
Q

Update the value of the second item in the following array using a new variable, and then log it to the console. Next, update the value without using a new variable.

let favPlaces = [‘Mallorca’, ‘NYC’];

A
let myHome = facPlaces[1]
console.log(myHome);

facPlaces[1] = ‘Westport’;
console.log(favPlaces[1]);

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

How would you find out how many items there are in the following array?

let favPlaces = [‘Mallorca’, ‘NYC’];

A

console.log(favPlaces.length);

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

How would you add one item to the following array and then how would you remove the last item.

let favPlaces = [‘Mallorca’, ‘NYC’];

A

favPlaces.push(‘Westport’);

favPlaces.pop();

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

For each of the following questions, use a method to do the following to the array below:

let groceryList = [‘orange juice’, ‘bananas’, ‘coffee beans’, ‘brown rice’, ‘pasta’, ‘coconut oil’, ‘plantains’];

  • Remove the first item from the array
  • Add an item to the start of the array
  • Now edit the list so that you can give your friend a list that only includes bananas, coffee beans and brown rice
A

groceryList.shift();
groceryList.unshift(‘popcorn’);
console.log(groceryList.slice(1, 4));

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

What are the differences between arrays that are assigned with let and ones which are assigned with const? Provide example with what you can and cannot do with arrays defined with const.

A

Arrays defined with let can be reassigned, const cannot. However, the contents of an array defined with const can still be changeable. For example you can and cannot do this:

Can:

const utensils = ['Fork', 'Knife'];
utensils.pop();

Cannot:

utensils = ‘Spoon’;
console.log(utensils);

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

If I have a variable containing a string, how can I easily modify the string so that they’re numbers?

A
var num = '53423';
parseInt(num);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a function, that takes a sentence that is a string as an argument and the function returns the longest word. Remember to not take punctuation into account. So for the exclamation points and periods, remove them.

A
function LongestWord(sen) {
sen = sen.split('');
for (var i = 0; i < sen.length; i++) {
if (sen[i] == '!' || sen[i] == '.') {
delete sen[i];
}
}
sen = sen.join('').split(' ');

sen.sort(function(a,b) {
return b.length-a.length;
});

return sen.splice(0,1).join(‘’);
}

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

Write a function, that takes a string as an arugment and makes every first letter in a word capital.

A
function LetterCapital(str) {
var word = str.toLowerCase().split('');
for (var i = 0; i < word.length; i++) {
word[i] = word[i].split('');
word[i][0] = word[i][0].toUpperCase();
word[i] = word[i].join('');
}
str = word.join(' ');
return str;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If have an array, how can I delete an element without knowing what index it will have not bothering about the index of the other elements, and how about bothering

A

arr.splice(i, 1);

or

delete arr[i];

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