Skip to content

Instantly share code, notes, and snippets.

@coetry
Created August 17, 2018 01:34
Show Gist options
  • Save coetry/1c384d4f57e37041dfedce575cae4986 to your computer and use it in GitHub Desktop.
Save coetry/1c384d4f57e37041dfedce575cae4986 to your computer and use it in GitHub Desktop.
// The following arrays will be used through the questions:
const words = [ 'cattywampus', 'snickersnee', 'lollygag', 'pandiculation', 'ratoon' ];
const numbers = [ 8, 3, 22, 0, -73, 15 ];
// Question 1:
// Use forEach to console.log all the words in the words array. Do not write a for loop.
function logWords(){
// words.forEach(word => console.log(word));
words.forEach(function(word){
console.log(word)
})
}
// Question 2: Use forEach to console.log the each number in the numbers array divided by two
// It should console.log:
// 4
// 1.5
// ...etc.
function logHalfNumbers(){
// numbers.forEach(number => console.log(number / 2))
numbers.forEach(function(number) {
console.log(number / 2)
})
}
// Question 3: Using the getFirstLetter function below as your callback,
// use map to create an array contaning the first letter of
// every word from the words array.
function getFirstLetter(word) {
// return word.charAt(0)
return word[0]
}
// const getFirstLetter = word => word[0]
// const getFirstLetter = word => word.charAt(0)
const firstLetters = words.map(getFirstLetter);
// Question 4: Use map to create an array containing the last letter of every word
// from the words array.
const lastLetters = words.map(function(word) {
// return word.chartAt(word.length - 1)
return word[word.length - 1]
});
// const lastLetters = words.map(word => word[word.length - 1])
// const lastLetters = words.map(word => word.charAt(word.length - 1))
// Question 5: Use map to create an array containing the reversed version of each word
// from the word array. For example, the first entry will be 'supmawyttac'.
const reversedWords = words.map(function(word) {
return word.split("").reverse.join("")
});
// const reversedWords = words.map(word => word.split("").reverse.join(""))
// Question 6: Use map to create an array containing the absolute value of every number
// in the numbers array.
const absoluteNumbers = numbers.map(function(number) {
if (number < 0) return -number
return number
});
// const absoluteNumbers = numbers.map(function(number) {
// if (number < 0) return number * - 1
// return number
// });
// const absoluteNumbers = numbers.map(function(number) {
// return Math.abs(number)
// });
// const absoluteNumbers = numbers.map(number => number < 0 ? -number : number);
// Question 7: Use map to create an array containing each number from the numbers array
// rounded to the nearest 10. For example: 10, 0, 20, 0, etc.
const roundedToTenNumbers = numbers.map(function(number) {
return Math.round(number) * 10
});
// const roundedToTenNumbers = numbers.map(n => Math.round(n) * 10);
// Question 8: Use map to create an array that contains only the words from the words
// array containing 's'. All other words should be replaced with null.
// For example: 'cattywampus', 'snickersnee', null, etc.
const wordsWithS = words.map(function(word) {
if (word.includes('s')) return word
return null
});
const wordsWithS = words.map(word => word.includes('s') ? word : null);
// Stretch Questions:
// The stretch question will use the array method reduce, which was only briefly reviewed
// in the lecture. Refer to online documentation for more information on reduce.
// Question 9: Use reduce to sum up all of the numbers in the numbers array.
function sum(currentTotal, nextNumber) {
return currentTotal + nextNumber
}
// const sum = (currentTotal, nextNumber) => currentTotal + nextNumber
const sumTotal = numbers.reduce(sum);
// Question 10: Use reduce to find the word from the words array that is last in the alphabet.
const alphabeticallyLast = words.reduce(function(last, current) {
if (last > current) return last
return current
});
// const alphabeticallyLast = words.reduce((last, current) => last < current ? last : current)
@haywood-d-johnson
Copy link

You ought to do them all in gist and set them up with V.1 ans and V.2 ans if you feel froggy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment