There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
// This will generate a random index in the artists array
const randomIndex =artist=> {
return Math.floor((Math.random() * artists.length));
}
//Each time we log artists array with a random index, it will give us a random artists (plus string interpolation)
console.log(${artists[randomIndex()]} is one of my favorite artists.);
In previous lessons you breifly went over arrow functions as a footnote, and only ever had us use them if we wanted to, depending on our preferred way of writing functions. Now this assignment is forcing us to use arrow functions and expects us to be proficient at them. I would’ve liked WAY more practice using arrow functions beforehand. This is where a khanacademy approach of drilling a concept before moving on would be nice.
Mapping is a one-to-one operation. The output array contains one element mapped from the corresponding element in the input array, applying the same operation to all input elements.
The function inside the argument is known as the callback function. We can simplify it using ES6 arrow syntax…
arr2 = arr1.map(x => x ** 2)
Note that the function performs the operation on the input elements, each in turn, then appends the value to the output array at the same index as the input value.
I dont understand the .filter() section of the code. Why is it outputting numbers? The function is saying to “return typeof thing === ‘number’;”. In my mind that saying return the exact value of the string ‘number’. Does ‘number’ (with quotation) mean actual numbers?
Hello, in bit43…'s question they say “return typeof thing === ‘number’;” - that may mean that in js the ‘number’ element/argument is a given that it means a boolean type of object? A predicate function is a standard js rule that if you ask " === ‘number’ " it asks if it is a boolean?What if your array’s element is indeed ‘number’?
number is the value returned from typeof object when object is numerical data.
The result of a comparison is boolean.
A predicate function returns a boolean, but does not necessarily ask if any values are boolean. In the case above, it will return true if the identity comparison is the same, and false otherwise.
I was looking through this thread and I had the same question. In the example you set above, the numbers for “fizz” and “buzz” were already set beforehand, but in the current example, the numbers 3.14 and 100 are not labeled as numbers, only [1,2,3,4,5] are.
artists.forEach(artist => {
console.log(artist + ' is one of my favorite artists.');
});
forEach method, why return is not used ? all other method have return. can i save this in to variable as other methods did in const. ? but when i did that and call that function output come as undefined.
How can I differintiate items in an array list with using iterators?
For example let’s take a list of favorite fruits from this lesson.
I want to assign a specific article to each element in the list:
const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
fruits.forEach(item=>
if (item.charAt(0) === 'a'||'e'||'o'||'i'||'u') {
console.log('I want to eat an '+ item);
} else {
console.log('I want to eat a '+ item));
After some searching I’ve desided that item.charAt(0) method should be good.
Combined it with if…else statement. But the code returns me an error saying that “if” token is unexpected.
Also tried using a ternary operator instead of if…else — it does run although it always gives the first expression.
What am I doing wrong, how can I make this code run smoothly and return me those precious correct articles??
Tried using the syntax you’ve provided and it does work! Not sure I understood your hint yet. It gave me an idea to try and use an unnecessary complicated function which theoretically should compare an additional array consisting of vowels against available items and make the condition become true in case there’s a match. It doesn’t work so far but I will experiment with it later when there’s more time on my hands.
const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
fruits.forEach(item=>{
const vowels = ['a','e','i','o','u','A','E','I','O','U']
if (vowels.forEach(vowel=>{
vowel === item.charAt(0)
})) {
console.log('I want to eat an '+ item);
} else {
console.log('I want to eat a '+ item);
}
});
Will always be, undefined. We cannot use Array.forEach() in a conditional since it has no return.
We’re dealing with strings, so let’s stick to strings. Array.forEach() does not work on strings, but, there is another even more purpose based iterator that does work on strings. Take another look at my hint, and go down the list of iterators we have just learned.
map
filter
includes
Which one best fits this scenario?
For our purposes, we do not need an array of vowels. Strings are iterable objects.
I learned traditional JS and later how it paired with jQuery. I’m regularly having to translate my older knowledge to ES6. Could somebody provide an example or two for how the syntax in this lesson would look if it were not written in Es6? The newer notation is unfortunately a difficult hurdle for me.