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`)
    }
   
}
1

1 Answer 1

0

You can make use of the typeof operator like this:

if (typeof guests === 'number') {
    // number
} else {
    console.log('not a number');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Might be improved by using Number.isInteger, assuming we can't have half guests.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.