Home > Blockchain >  i wanna ignore the string argument?
i wanna ignore the string argument?

Time:09-06

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:

  1. There is no float type in JavaScript. You have to check somehow if it's float Math.trunc(num) !== num for example.
  2. You wanted to add calc[i] not calc, because calc is an array.
  3. Are you sure you wanted to use trunc? Usually round, ceil or floor 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

  • Related