0

how do I write a defensive line of code stating a parameter must be a number or else console.log 'it must be a number

function getDiscount(taxBoolean,guests) {
    getPrices(taxBoolean)
    
    let condition1 = typeof(guests) == 0
    let condition2 = condition1 > 0 && condition1 < 30
    if (condition1 && condition2) {
        let discount = 0
        if (guests < 5) {
            discount = 5
        }
        else if (guests >= 5) {
            discount = 10
        }
 console.log(`Discount is : $${discount}`)
    }
     else{
        console.log(`the secound argument must be a number between 0 and 30`)
    }
   
}
Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38
Ighodan
  • 9
  • 2
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/a/261593/3082296) – adiga Jan 12 '23 at 07:42

1 Answers1

0

You can make use of the typeof operator like this:

if (typeof guests === 'number') {
    // number
} else {
    console.log('not a number');
}
Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38