JavaScript Rest operator [duplicate] - javascript

This question already has answers here:
Spread Syntax ES6
(8 answers)
Closed 7 months ago.
I am trying to learn javascript but having trouble in the spread and rest operators.
Can't understand whats happening here how dose this take in the taxRate parameter like a singel number when we spred the itemsBought parameter
function addTaxToPrices (taxRate, ...itemsBought)
{
return(itemsBought.map(item => item*taxRate));
}
let ShoppingCart=addTaxToPrices(1.1,46,89,35,79);
console.log(ShoppingCart)

for my understanding it is nearly the same than if you do:
function addTaxToPrices (taxRate, itemsBought)
{
return(itemsBought.map(item => item*taxRate));
}
itemsBought = [46,89,35,79]
let ShoppingCart=addTaxToPrices(1.1,itemsBought);
console.log(ShoppingCart)

Related

Why does every() method work only one line? [duplicate]

This question already has answers here:
Why doesn't my arrow function return a value?
(1 answer)
Curly Brackets in Arrow Functions
(3 answers)
When should I use a return statement in ES6 arrow functions
(6 answers)
Closed 1 year ago.
The following code works:
let check = checkingImages.every((item)=>item.classList.contains('matched'))
But this one doesn't:
let check = checkingImages.every((item)=>{
item.classList.contains('matched')
})
Can someone explain what's the reason for the second code not working, please?

What does ‘...’ in JavaScript do? [duplicate]

This question already has answers here:
What "..." means in Javascript (ES6)? [duplicate]
(1 answer)
Spread Syntax vs Rest Parameter in ES2015 / ES6
(11 answers)
Closed 4 years ago.
I’m new to coding and slef teaching JavaScript.
My task I was set was to identify the largest value in an array.
My soloition works, but I needed the ‘...’ for it to work properly.
My question is, what does the ‘...’ in the last line actually mean/do?
function createAnArr(){
let myArr = prompt("Enter your numbers separated by commas:").split(",");
return myArr;
}
console.log(Math.max(...createAnArr()));
'...' it means it will be able to take any number of arguments of the respective scope
'...': The spread operator is used for array construction and destructuring, and to fill function arguments from an array on invocation. A case when the operator spreads the array (or iterable object) elements.
more details you can see here

how does arrow function with spread operator work [duplicate]

This question already has answers here:
What do multiple arrow functions mean in JavaScript?
(7 answers)
How does the spread syntax in ES6 JS work as a function argument?
(1 answer)
What are these three dots in React doing?
(23 answers)
Closed 5 years ago.
How does this work:
const invert = fn => (...args) => -fn(...args);
Specifically, what is happening with (...args). It behaves as if it is separating the arguments from the function after the first fat arrow, but I suspect there is a better, more specific, explanation. Thanks.

How to create a repeatString Function [duplicate]

This question already has answers here:
Repeat String - Javascript [duplicate]
(30 answers)
How can I repeat strings in JavaScript? [duplicate]
(5 answers)
Closed 5 years ago.
so I am very new to js and I'm having a really hard time comprehending the language. Could anyone take 5 to look at this code and tell me why my function isn't working...? The goal is to create a function that duplicates a string by the number inputted into the function.
function repeatString(string, num) {
return string*num
}
var output=repeatString("Hello!",2);
console.log(output);
You could simply call the repeat method:
function repeatString(string, num) {
return string.repeat(num);
}

Angular2 Typescript "= () =>" [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 6 years ago.
Easy question for someone to answer, what happens in this typescript function?
getTweetCount = () => this.data.totalCount;
data.totalCount is of type number from a service.
getTweetCount = () => this.data.totalCount;
You are creating an "arrow function" that returns this.data.totalCount. There are lots of docs about arrow functions out there.
More
Arrow functions are also covered here : https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html

Categories