Repeat a string multiple times in an assignment [duplicate] - javascript

This question already has answers here:
Repeat a string in JavaScript a number of times
(24 answers)
Closed 3 years ago.
With numbers you can multiply a number a certain number of times using the following code:
var y = 10;
var x = y * 3; // 30
Is there a way to do the same with strings with native methods?
var y = "test ";
var x = y * 3; // test test test
Or if not is there a succinct way to do this in a single line?

You could do:
var y = "test ";
y = y.repeat(3);
And this would give you "test test test "
String.repeat()

What would be "string"*3 in Python, can be done with "string".repeat(3) in Javascript. That appears to be what you want.

Related

How do I make this function a number? [duplicate]

This question already has answers here:
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed last year.
I'm learning JavaScript and I wanted to accept user input and add a number to it ,but instead of outputting 12+12 = 24 it's outputting 12+ 12 = 1212.
document.getElementById("mybutton").onclick = function(){
var myAge = document.getElementById("mytext").value + 12;
document.getElementById("value").innerHTML = myAge;
}
I tried doing:
myName = Number(myName)
but it didn't work
In this case use a function parseInt()
example:
document.getElementById("mybutton").onclick = function(){
var myAge = parseInt(document.getElementById("mytext").value) + 12;
document.getElementById("value").innerHTML = myAge;
}

Problem calculating the sum of arithmetic progression when using prompt() [duplicate]

This question already has answers here:
How to get numeric value from a prompt box? [duplicate]
(6 answers)
Javascript: "+" sign concatenates instead of giving sum of variables
(4 answers)
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed 2 years ago.
I am trying this code to add a number starting from 1 to n using this mathematical equation:n = n(n+1) /2
var n = prompt("enter a number");
function adding(n) {
let N = n * (n + 1) / 2;
return N;
}
alert(adding(n));
but it's not working in prompt. It works like this:
function adding(n) {
let N = n * (n + 1) / 2;
return N;
}
console.log("answer", adding(100));

when taken 5 root of big number gives wrong answere in js [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 2 years ago.
When I take the 5 root of a big number, js gives me a wrong answer
var pow = 5;
var A = 133;
var B = 110;
var C = 84;
var D = 27;
var E;
E = Math.pow((Math.pow(A, pow) + Math.pow(B, pow) + Math.pow(C, pow) + Math.pow(D, pow)), 1/pow);
console.log(E)
This should give 144 but gives 144.00000000000003
5 root of 61917364224 = 144

How subtraction of two float values in JavaScript? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have an issue with the subtraction of two values. when I set discount_amt value to 2.5 then total return me 0.5 but when discount_amt is set to 2.6 it return 0.3999999999999999 instead of 0.4 why?
var total = parseFloat('3').toFixed(1);
var discount_amt = parseFloat('2.6').toFixed(1);
total = total - discount_amt;
console.log(total);
var total = parseFloat('3').toFixed(1);
var discount_amt = parseFloat('2.6').toFixed(1);
total = total - discount_amt;
console.log(total);
This seems to fix it. You forget parsefloat() and tofixed()
total = 3;
discount_amt = 2.6;
console.log(parseFloat(total).toFixed(1) + ' ' + parseFloat(discount_amt).toFixed(1));
total = parseFloat(total).toFixed(1) - parseFloat(discount_amt).toFixed(1);
console.log(parseFloat(total).toFixed(1));
Explanation why floats are handled this way: answer or directly to the link that answer refers to link

Adding two integer JavaScript [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I am trying to add two numbers in JavaScript, but the result is inexact. This my example:
var x = 11.12, y = 11.07;
console.log(x + y); // the result is 22.189999999999998 but the real result is 22.19
Any solution for this?
This is what you need to do:
var x = 11.12, y = 11.07;
var result = (parseFloat(x) + parseFloat(y)).toFixed(2);
console.log( result );

Categories

Resources