I need to multiply any arguments I add, I want to ignore the string arguments and if I put a float number, I need to turn it into an integer first and then multiplay.
function multiply(...calc){
let total = 1;
for(let i = 0; i < calc.length; i ) {
//
if(typeof calc === 'string') {
continue;
} else if (typeof calc === 'float') {
Math.trunc(calc)
} else {
total *= calc[i]
}
}
return (`The Total IS : ${total}`)
}
// nedded out put ==>
console.log(multiply(10, 20)); // 200
multiply(10, 100.5); // 200
console.log(multiply("A", 10, 30)); // 300
multiply(100.5, 10, "B"); // 1000
// here is the output i get it ==>
The Total IS : 200
The Total IS : NaN
The Total IS : NaN
CodePudding user response:
- There is no
float
type in JavaScript. You have to check somehow if it's floatMath.trunc(num) !== num
for example. - You wanted to add
calc[i]
notcalc
, becausecalc
is an array. - Are you sure you wanted to use
trunc
? Usuallyround
,ceil
orfloor
are used to convert float to int.
function multiply(...calc) {
let total = 1;
for (let i = 0; i < calc.length; i ) {
if (typeof calc[i] === 'string') {
continue;
} else if (Math.trunc(calc[i]) !== calc[i]) {
total *= Math.trunc(calc[i])
} else {
total *= calc[i]
}
}
return (`The Total IS : ${total}`)
}
console.log(multiply(10, 20)); // 200
console.log(multiply(10, 100.5)); // 100
console.log(multiply("A", 10, 30)); // 300
console.log(multiply(100.5, 10, "B")); // 1000