How to create a repeatString Function [duplicate] - javascript

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);
}

Related

JavaScript Rest operator [duplicate]

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)

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?

Got undefined as output. What is going wrong with this code? [duplicate]

This question already has answers here:
Function with forEach returns undefined even with return statement
(5 answers)
What does `return` keyword mean inside `forEach` function? [duplicate]
(2 answers)
Can forEach in JavaScript make a return? [duplicate]
(6 answers)
Closed 1 year ago.
Here arr is array target is any number. I wants to return two numbers of arr. Sum of those number equals to value stored in target.
function twoSum(arr, target) {
var x= arr.forEach(e=>{
return arr.forEach(num=>{
if((e+num)===target){
return [e,num];
}
})
})
return x;
}

JavaScript object properties copy syntax [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
What do {curly braces} around javascript variable name mean [duplicate]
(1 answer)
Closed 4 years ago.
I was reading some JavaScript code and saw a line of code similar to this.
const {name, password, email} = user;
I tried searching around but could not figure out what this syntax is called so I am not able find the documentation. Is this a new JavaScript syntax? What is it called? What does it do exactly?

JavaScript semicolon after a function [duplicate]

This question already has answers here:
Why should I use a semicolon after every function in javascript?
(9 answers)
Do we need semicolon at the end? [duplicate]
(6 answers)
Closed 9 years ago.
I wonder the difference between two codes below in JavaScript, welcome answer.
Code 1:
function Add(a,b) {
return a + b;
}
Code 2:
function Add(a,b) {
return a + b;
};
The second code contains an empty statement. You can add as many as you want.
function Add(a,b) {
;;;;;
return a + b;;;;;;;
};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
First one is the standard method
Second one is not recommended.. Use the first method
Further reading - Why should I use a semicolon after every function in javascript?
There is no difference. The ; in the second example just creates an empty statement there.

Categories