Day 10
Today I will be learning about iterators. .forEach() is a nice iterator. I like it.
const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
fruits.forEach(fruit => console.log(`I want to eat a ${fruit}.`));
Finding it hard to keep up with function declaration, expression and arrow notation. Find myself going over past examples to refresh my memory a lot.
.map()
const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
const secretMessage = animals.map(letter => {
return letter[0];
});
console.log(secretMessage.join(''));
const bigNumbers = [100, 200, 300, 400, 500];
const smallNumbers = bigNumbers.map(number => {
return number / 100;
});
I feel like I’m getting the hang of arrow notation again. Here’s some .filter() very similar to .map.
const randomNumbers = [375, 200, 3.14, 7, 13, 852];
const smallNumbers = randomNumbers.filter(number => {
return number < 250;
});
const favoriteWords = ['nostalgia', 'hyperbole', 'fervent', 'esoteric', 'serene'];
const longFavoriteWords = favoriteWords.filter(word => {
return word.length > 7;
});
Breezed through the .findIndex() work:
const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant'];
const foundAnimal = animals.findIndex(animal => {
return animal === "elephant";
});
const startsWithS = animals.findIndex(animal => {
return animal[0] === "s";
});
Enjoying the .reduce() exercise too! It’s funny how the moment something clicks my enjoyment shoots up. I love the feeling of learning!
const newNumbers = [1, 3, 5, 7];
const newSum = newNumbers.reduce((accumulator, currentValue) => {
console.log('The value of accumulator: ', accumulator);
console.log('The value of currentValue: ', currentValue);
return accumulator + currentValue;
}, 10);
console.log(newSum);
Fixing and filling in incomplete code to turn this:
const words = ['unique', 'uncanny', 'pique', 'oxymoron', 'guise'];
// Something is missing in the method call below
console.log(words.some(() => {
return word.length < 6;
}));
// Use filter to create a new array
// Make sure to uncomment the code below and fix the incorrect code before running it
// console.log(interestingWords.every((word) => { } ));
Into this:
const words = ['unique', 'uncanny', 'pique', 'oxymoron', 'guise'];
console.log(words.some((word) => {
return word.length < 6;
}));
const interestingWords = words.filter(word => {
return word.length > 5;
});
console.log(interestingWords.every((word) => {
return word.length > 5;
} ));
Final exercise of the day, choosing which method each bit of code should use:
const cities = ['Orlando', 'Dubai', 'Edinburgh', 'Chennai', 'Accra', 'Denver', 'Eskisehir', 'Medellin', 'Yokohama'];
const nums = [1, 50, 75, 200, 350, 525, 1000];
// Choose a method that will return undefined
cities.forEach(city => console.log('Have you visited ' + city + '?'));
// Choose a method that will return a new array
const longCities = cities.filter(city => city.length > 7);
// Choose a method that will return a single value
const word = cities.reduce((acc, currVal) => {
return acc + currVal[0]
}, "C");
console.log(word)
// Choose a method that will return a new array
const smallerNums = nums.map(num => num - 5);
// Choose a method that will return a boolean value
nums.some(num => num < 0);
I’m back to enjoying myself again and feeling the frustration melt away. I can tell this journey is going to be a loop of this experience and I will go through many iterations of it!