“The code below so far”
Could you explain how the code below works?
and Thanks all you guys
function multiply(multiplier, ...theArgs) {
return theArgs.map(function(elements) {
return multiplier * elements;
});
}
“The code below so far”
Could you explain how the code below works?
and Thanks all you guys
function multiply(multiplier, ...theArgs) {
return theArgs.map(function(elements) {
return multiplier * elements;
});
}
Hello there,
I would explain it with what I would have done to make this function:
function multiply(multiplier, vec) {
return vec.map(element => multiplier*element);
}
multiply(3, [1, 2, 3, 4]); // returns: [3, 6, 9, 12]
If that is not too clear either, then I suggest you take a look at the main syntax used in the code you mentioned:
Spread Operator:
– https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/copy-an-array-with-the-spread-operator
– https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/combine-arrays-with-the-spread-operator
Array.prototype.map method:
– https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-map-method-to-extract-data-from-an-array
Arrow function (what I used):
– https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions
Hope this helps