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

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

Related

Adding commas to number toFixed toLocaleString [duplicate]

This question already has answers here:
JavaScript: Format whole numbers using toLocaleString()
(4 answers)
Closed 1 year ago.
I am creating a calculator and need guidance on adding thousand separators to a whole number.
//logic
var portVal = 100000000;
var rateUSD = 1.3654;
var sustPortBase = 22;
var avgPortBase = 66;
var sustPortCarb = (portVal*rateUSD/1000000)*sustPortBase;
var avgPortCarb = (portVal*rateUSD/1000000)*avgPortBase;
var diff = avgPortCarb - sustPortCarb;
var nbHomes = diff * 0.115;
var nbCars = diff * 0.216;
var nbTrees = diff * 16.5;
var nbPlanes = diff * 0.833359;
var avgPortCarbRound = avgPortCarb.toFixed();
var sustPortCarbRound = sustPortCarb.toFixed();
console.log("Cars:"+(nbCars).toFixed());
console.log("Cars Decimals:"+(nbCars).toLocaleString('en'))
Outputs:
Cars:1298
Cars Decimals:1,297.676
I would like to keep only thousand seperators but nothing after the (dot.) i.e. 1,297
https://jsfiddle.net/ge5oj2xk/1/
Try using Math.floor function.
console.log("Cars Decimals:"+Math.floor(nbCars).toLocaleString('en'))

Repeat a string multiple times in an assignment [duplicate]

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.

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

Why is my number coming up as 0? [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 6 years ago.
I am trying to make a calculator that calculates the amount of calories that have been burned in a certain amount of time, but whenever I run it, I get 0 as the output. I have tried setting the calorieCountOut variable to an arbitrary number, and then it works fine, but every time I run it with this code here, I get 0. Here is my code:
const AGECONST = 0.2017;
const WEIGHTCONST = 0.09036;
const HRCONST = 0.6309;
const SUBTRACTCONST = 55.0969;
const TIMECONST = 4.184;
//var gender = document.getElementById("gender").innerHTML;
var gender = "male";
var weight = document.getElementById("weight");
var age = document.getElementById("age");
var time = document.getElementById("time");
var hr = 140;//dummy number
function calculate(){
if (gender = "male"){
var calorieCount = ((age * AGECONST) - (weight * WEIGHTCONST) + (hr * HRCONST) - SUBTRACTCONST) * time / TIMECONST;
}
//else if (gender = "female"){
//}
var calorieCountOut = calorieCount.toString();
document.getElementById("output").innerHTML = calorieCountOut;
}
Try getElementById('myId').value for the value inside the control instead of the control itself as an object.
Right now you assign a html object to a variable, but what you want (i assume) is the number stored in that html object.

How to add a comma in my increasing number via JS? [duplicate]

This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 8 years ago.
I'm trying to add a comma between my 20000 to show as 20,000 without it messing up and have gotten pretty close but trying to fill in the next steps. Below, I've listed my code with the function thats able to do it but trying to connect the two together to properly work.
Here is my jsFiddle!
And code..
HTML
<span id="liveNumbers">23000</span>
JS
setInterval(function(){
random = (Math.floor((Math.random()*2)+1));
var plus = Math.random() < 0.5 ? 1 : 1;
random = random * plus;
currentnumber = document.getElementById('liveNumbers');
document.getElementById('liveNumbers').innerHTML = parseInt(currentnumber.innerHTML) + random;
}, 3000);
function commaSeparateNumber(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
return val;
}
You'd have to change your initial value to include a comma or do an initial run before the setInterval is started but something like this might work:
setInterval(function(){
random = (Math.floor((Math.random()*2)+1));
var plus = Math.random() < 0.5 ? 1 : 1;
random = random * plus;
currentnumber = document.getElementById('liveNumbers');
var curnum = parseInt(currentnumber.innerHTML.replace(",",""));
document.getElementById('liveNumbers').innerHTML =
commaSeparateNumber(curnum + random);
}, 3000);
function commaSeparateNumber(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
return val;
}

Categories

Resources