Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i wanna ignore the string argument?

hi: I’m trying to solve this problem, 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

the example below …

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

  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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading