Hello and welcome to StackOverflow.
First of all, I think a good point to start to understand the syntaxis is checking the w3 examples: filter, map, reduce.
Or mozilla examples: filter, map, reduce
Try these examples and try to understand each step.
Your exercise need for these three functions, so as an overview, we can define each one in this way (maybe this is so informal):
- Filter: Use a filter to create a new array.
- Map: Do something thorugh every value into the array (i.e. map the values from one array to another).
- Reduce: Reduce the array to an unique element.
So... for your exercise.
The phrases itself explain what function to use. Check this:
I need find all uniques elements and minus than 10
It means: "I need to use filter
with a function like return unique && x < 10
."
Then do the sumatory
So... reduce to an unique element. Is like "Then do reduce
with a function like return currentValue + nextValue
And later multiply each array value by this number
That's like: "And later map every value as value*=sumatory
"
And that's all! Easy, right? Now let's translate into code.
Let's assuming you have an array like this: [10, 11, 1, 2, 3, 4]
(the last into the example)
First you need to find the unique value and < 10 using filter
.
The unique position has a trick. If you compare the first and last index for a number and it's the same value, then... is unique. So the function is like this
var array = [10, 11, 1, 2, 3, 4]
var filter = array.filter(x => (array.indexOf(x) === array.lastIndexOf(x) && x < 10))
console.log(filter) // [ 1, 2, 3, 4 ]
But there is no only one way to do this. You can create a Set
to no repeat values, count every value to get those with count=1
or whatever, is your decission. I've chosen this because I think is the best way.
The syntaxis is easy to read: For each value into the array, named x
then do =>
the function. In this case compare the index and compare is <10
.
Note that in yor example you are missing (x) =>
so you can't pass variables to your function.
Next step... reduce
var reduce = filter.reduce((a, b) => a + b, 0)
console.log(reduce) // 10
Easy. Use reduce
into the output array.
The sintaxys here is: "With the current value a
and next value b
, do =>
the function a+b
starting at 0
". In other words... do the sumatory.
The last step: Map
.
var map = array.map((m) => {
return m * reduce
})
console.log(map) // [ 100, 110, 10, 20, 30, 40 ]
Here you are saying: "For every value into the array, m
, do a function =>{...}
where multiply by the sumatory m * reduce
."
AND NOW: DO IT TOGETHER!!
Yes, the (really) last step, only replace variables for functions.
Where you see reduce
put filter.reduce((a, b) => a + b, 0)
.
And where there is filter
set array.filter(x => (array.indexOf(x) === array.lastIndexOf(x) && x < 10))
var map = array.map((m) => {
return m * (array.filter(x => (array.indexOf(x) === array.lastIndexOf(x) && x < 10))).reduce((a, b) => a + b, 0)
})
And that's all.
Hope it helps to understand a little bit how to use the functions and how to solve these problems.
After see what have you try, check the examples and note that you need to get the variables using (var) =>
.
And a snippet
var array = [10, 11, 1, 2, 3, 4]
var map = array.map((m) => {
return m * (array.filter(x => (array.indexOf(x) === array.lastIndexOf(x) && x < 10))).reduce((a, b) => a + b, 0)
})
console.log(map)