Brainfarting over simple if/else if

Brainfarting over simple if/else if
0

#1

Thanks! Found the solution, I simply forgot to add the *

I feel so stupid now ;D


#2

Show us something you’ve tried - it may be a simple error.


#4

You’re nearly there…

receive = children*a+(children-4)c+(children-2)b;

This line is missing some operators. (children-4)c is not the same as (children-4) * c.

I added some operators and extra parentheses and found your code basically works.

Edit
I should note, I added the parens just for readability - they are not necessary :slight_smile:


#5

Hope this help:

var allowance = [25, 12.5, 7.5]; //using array, not too much variables.
var recieved;
var child = prompt("Amount of children?");

if (child < 3) {
    recieved = allowance[0]*child;
} else if (child < 5) { // you don't need '&& child > 2', it already evaluated before
    //easily readable to add parentheses for each group of allowance.
    recieved = (allowance[0] * 2) + (allowance[1]*child-2);
} else {
    recieved = (allowance[0] * 2) + (allowance[1] * 2) + (allowance[2]*child-4);
}

//you might want to alert after the code finish giving the right value to recieved
alert("you will recieve $" + recieved);