Reputation: 33
Say I'm creating a function to add some numbers together, and I want to verify they are all actually numbers.
I am doing this:
function addNumbers(x, y) {
if (typeof x == 'number' || typeof y == 'number') {
// do something..
}
}
It seems unpractical if I had more than two numbers. What would be a better way to check for multiple numbers?
Upvotes: 3
Views: 932
Reputation: 53874
I think its more readable form, use Array.prototype.every
, Number.isInteger
and Array.prototype.reduce
.
I'm not sure what you want to do with errors, so we just logging them (and the result);
// consider floating point
const isNumber = n => typeof n == 'number' && !isNaN(n)
const addNumbers = (...args) => {
const isValid = args.every(isNumber);
// const isValid = args.every(Number.isInteger);
if (!isValid) {
console.log('Error');
return;
}
const sum = args.reduce((sum, curr) => sum + curr, 0);
console.log(sum);
return sum;
}
addNumbers(5, 6);
addNumbers(5, 6, NaN);
addNumbers(5, '6');
addNumbers('5', '6');
addNumbers('5', 6);
addNumbers(5.5, 6);
addNumbers(5.5, 6, 6.4, 65);
addNumbers(5.5, 6, {});
Upvotes: 1
Reputation: 22766
You can put them in an array and use a Boolean flag to check if all are numbers, using Array.prototype.every
(and check for NaN
s, because typeof NaN === 'number'
):
function addNumbers(...args) {
var all_numbers = args.every(a => typeof a == 'number' && !isNaN(a));
if (all_numbers) {
var sum = 0;
args.forEach(n => sum += n);
console.log(sum);
} else {
console.log('something is not right!');
}
}
addNumbers(5, 6);
addNumbers(5, 6.2);
addNumbers(5, 6, NaN);
addNumbers(5, 6, []);
addNumbers(5, 6, {});
addNumbers(5, '6');
addNumbers('5', 6);
Upvotes: 3
Reputation: 16908
You can use get your inputs as an array using the ...
rest parameter syntax then use Array.prototype.reduce
to sum them and while doing so you can convert the elements to numbers using the +
operator and add them:
function addNumbers(...nums) {
return nums.reduce((sum, num) => sum + +num)
}
console.log(addNumbers(1, 2, "3", 4));
Or if you want to skip the non-numbers (which will produce NaN
if you use the first code snippet) just check the type before adding, if number you're good else replace that with a 0
:
function addNumbers(...nums) {
return nums.reduce((sum, num) => sum + (!(typeof(num) === "number") ? 0 : +num));
}
console.log(addNumbers(1, 2, "3", 4, "non-number"));
Upvotes: 1
Reputation: 6491
If performance not the issue here:
function testN(){
if(!Array.from(arguments).every((d)=>typeof d === "number")){
return;
}
console.log("pass");
//do something;
}
replace "do something" with whatever you want to do. Can use negated "some" method, that'll terminate earlier. Whichever fits you.
Upvotes: 0
Reputation:
You can push all your numbers into and array and run a loop over that array and add them.
let arr = [1,2,3,4]; #be it your array
function addNumbers(arr) {
let sum=0;
arr.map(item => {
if(typeof item === 'number') {
sum=sum+item;
}
else {
return item+' is not a number in given array';
}
return sum;
});
Upvotes: 0