vendredi 22 janvier 2021

Multiple criteria with including 'or' in Javascript if-else statement

I'm newbie in Javascript. Trying to write a function in which I will enter value for three items and it will return the total price. If I enter negative value for any parameter than it will give an error message instead of total price.

Expected results pseudocode:

if pen < 0 or pencil < 0 or sharpner < 0 then errorMessage else totalPrice

My code is :

function costCalc (pen, pencil, sharpner){

var penPrice = pen * 10;
var pencilPrice = pencil * 20;
var sharpnerPrice = sharpner * 5;

if (penPrice < 0 || pencilPrice < 0 || sharpnerPrice < 0 ){
    return "Something went wrong."
} else{
    var totalPrice = penPrice + pencilPrice + sharpnerPrice;
    return totalPrice
    }
}

I've 'return' twice here. Is this code ok? Or, I've to change something?

Advance thanks for your cooperation.

Regards.

Aucun commentaire:

Enregistrer un commentaire