This question already has answers here:
How do you check that a number is NaN in JavaScript?
(33 answers)
Closed 5 years ago.
I'm trying to check to see if an input is NaN, basically I want to alert 0 if nums doesn't have anything in it, or if that something is a 0. Any ideas on how to go about this?
var sumofnums = 0,
nums = document.getElementById("nums").value.split(",");
function add(){
for (i = 0; i < nums.length; i++) {
sumofnums += parseInt(nums[i]);
};
document.getElementById("sum").innerHTML = sumofnums;
};
if (nums === ''){
alert('0');
}
isNaN();
Google can help as well.
Related
This question already has answers here:
Why does console.log return undefined after correct output?
(1 answer)
Javascript even and odd range
(6 answers)
How to append something to an array?
(30 answers)
Closed 9 months ago.
I am creating a function which takes in a minimum and max value. The output should list all the even numbers between these two parameters. I have attempted at creating an array to display these numbers but I get this output:
evenNumbers(4,13) returns: undefined
evenNumbers(3,10) returns: undefined
evenNumbers(8,21) returns: undefined
How to fix my code so it shows the list properly?
var evenNumbers = function(minNumber, maxNumber){
var list = [];
for (let i = minNumber; i < maxNumber; i++){
if (i % 2 == 0){
for(let k = 0; k < maxNumber; k++){
i = list[k];
}
}
}
return console.log(list);
}
console.log('evenNumbers(4,13) returns: ' + evenNumbers(4,13));
console.log('evenNumbers(3,10) returns: ' + evenNumbers(3,10));
console.log('evenNumbers(8,21) returns: ' + evenNumbers(8,21));
This question already has answers here:
How to find the sum of an array of numbers
(59 answers)
Closed 6 years ago.
I'm trying to find a way to sum all the numbers that have been added to an array. I believe this should work:
var total = 0;
for (var i = 0; i < totalPrice.length; i++);
total += totalPrice[i];
document.getElementById("displayPrice").innerHTML = total;
But total comes out as a NaN.
This is an example of my code in JSFiddle, if you add the item twice the value gets pushed into the array, what am I doing wrong with the for loop?
https://jsfiddle.net/bgv5s9re/2/
You could use brackets
var total = 0;
for (var i = 0; i < totalPrice.length; i++){
total += totalPrice[i];
}
document.getElementById("displayPrice").innerHTML = total;
This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 6 years ago.
The first thing i want to ask is if there is a more efficient way of writing this programm of mine. The second question is why on earth after a specific number of characters the programm does not function well and prints zeros.
function toCurrency(price) {
var currencyToString = price.toString();
var finalPrice = "";
var counterComma = 0;
for (var i = 0; i < currencyToString.length; i++) {
counterComma++;
finalPrice += currencyToString[i];
if(counterComma == 3){
counterComma = 0;
finalPrice += ",";
}
}
return finalPrice;
}
console.log(toCurrency(123456253635423197874));
https://jsfiddle.net/DimitriXd4/68epen4n/
This question already has answers here:
JavaScript Number Split into individual digits
(30 answers)
Closed 6 years ago.
For instance, if i have the number 4444. I need it to do 4+4+4+4=16.
i want to get the result of 16. I tried changing the number to a string and got the array. But i don't know how to add it after. I searched it up but the other examples were too complicated for my level.
Working Example
var n = 4444;
var a = n.toString().split('').map(Number).reduce(function(a, b) {
return a + b;
});
Turn number to string
Split it
Map over array to return Numbers
Reduce to get total
or with a loop:
var sum = 0;
var string = n.toString();
for (var i = 0; i < string.length; i++) {
sum = sum + Number(string[i]);
}
This question already has answers here:
How to avoid scientific notation for large numbers in JavaScript?
(27 answers)
Closed 6 years ago.
e.g.Number.MAX_VALUE.toString() is "1.7976931348623157e+308"
I hope there is no e+308,How to achieve this ?
You could do like this:
var n = Number.MAX_VALUE.toString();
var parts = n.split("e+");
var first = parts[0].replace('.', "");
var zeroes = parseInt(parts[1], 10) - (first.length - 1);
for(var i = 0; i < zeroes; i++){ first += "0"; }
// => first === "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"