0

Here is the assignment so it's clear what I'm trying to do.

Write a program that calculates the price of a hotel room based on three factors. First, how many will be in one room: 1-2, 3-4, or 5-6? No more than six are allowed in a room. Second, are the guests members of AAA? Third, do they want a room with a view? The rate is calculated as follows:

Basic room rate is: 1-2 people = $50/night 3-4 people = $60/night 5-6 people = $70/night There is a discount for AAA members off of the basic room rate per night: 1-2 people = 15% 3-4 people = 10% 5-6 people = 5% A room with a view costs 10% more per night after all other calculations are performed.

The program should prompt the user for all the inputs, perform the calculations and output the total cost per night. It is suggested to use at least some nested if/else structures to calculate the cost.

Debuggers like firebug and JSLint haven't been any help and I suspect I'm just doing something completely wrong, though I haven't had trouble with "nested if" logic assignments before. Regardless I am a complete and utter newby.

When I test by inputting 1 for numberOfGuests, N for tripleAStatus, and N for roomView, finalRate is returning as isNaN (I know this means not a number), and I can't figure out why.

//Variable Declaration

var numberOfPeople;
var tripleAStatus;
var roomView;
var discountRate;
var roomRate;
var finalRate;
var discountPercent;

//Input

numberOfPeople = prompt("How many guests will their be? 6 Maximum.");


tripleAStatus = prompt("Are you a AAA Member? Y/N.");

roomView = prompt("Would you like your room to have a view?");



//Logic

if ((numberOfPeople <= 2) && (numberOfPeople > 0)) {
   roomRate = 50;
   discountPercent = .15;
} else if ((numberOfPeople <= 4) && (numberOfPeople > 2)) {
   roomRate = 60;
   discountPercent = .10;
} else if ((numberOfPeople <= 5) && (numberOfPeople > 4)) {
   roomRate = 70;
   discountPercent = .5;
} else {
   alert("Your number of guests must be at least 1 and no more than 6");
}

if (tripleAStatus = "Y") {
   discountRate = roomRate - (roomRate * discountRate);
} else if (tripleAStatus = "N") {
   discountRate = roomRate;
} else {
   alert("You need to answer with either Y or N");
}

if (roomView = "Y") {
   finalRate = (discountRate) + ((discountRate) * .10);
} else if (roomView = "N") {
   finalRate = discountRate;
} else {
   alert("You need to answer with either Y or N");
}

//Output

document.write("Total cost per night is " + "$" + finalRate);
Igor
  • 33,276
  • 14
  • 79
  • 112
  • 2
    You're assigning to variables during your if-statements. JavaScript uses `==` and `===` for testing equality. – Eric Oct 17 '12 at 23:47
  • 3
    Your NaN ("not a number") probably comes from the fact that you use the variable `discountRate` before it's ever assigned a value in this statement: `discountRate = roomRate - (roomRate * discountRate);` – Cameron Oct 17 '12 at 23:50

6 Answers6

2

It looks like

discountRate = roomRate - (roomRate * discountRate);

Should read

discountRate = roomRate - (roomRate * discountPercent);

Which is why you're getting NaN in finalRate; discountRate hasn't been defined at this point, so your code actually reads discountRate = 1 - (1 * undefined).

As other posters have mentioned, you also need to change your conditionals to use == or ===; = is an assignment operator, so rather than checking if tripleAStatus is "Y", you're actually checking if "Y" evaluates to true (which it always will).

tripleAStatus = 'N';
if (tripleAStatus = 'Y') {
    // tripleAStatus is now "Y", and the code inside this if will always be executed
}

Working changes: http://jsfiddle.net/5ZVkE/

Kelvin
  • 5,227
  • 1
  • 23
  • 36
  • 1
    Thank you! Total oversight on my part, the way = works in JS totally slipped my mind and wasn't coming back. I also learned a lesson in not making my variable names too similar because I was never able to catch the discountRate/discountPercent mixup. – user1754744 Oct 18 '12 at 17:25
  • Glad to help! I'd suggest having a read of http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use for a discussion of why you should almost always use `===`, and how it differs from `==` :) – Kelvin Oct 18 '12 at 20:46
0

In your If statements, you are assigning values by using single "=". Since the assignment always occurs, it will return true. In order to compare values, you need to use double "==" in the if statement. like this:

if (tripleAStatus == "Y") {
   discountRate = roomRate - (roomRate * discountRate);
} else if (tripleAStatus == "N") {
   discountRate = roomRate;
} else {
   alert("You need to answer with either Y or N");
}
solidau
  • 4,021
  • 3
  • 24
  • 45
  • 2
    Note that assignment doesn't return true on success as you suggest (it can't really fail), but rather returns the value that was assigned, which happens to be non-false (0, null, undefined, etc.) and therefore true. – Cameron Oct 17 '12 at 23:52
0

Replace the single '=' in your 'if' statements with '=='. You're currently using assignment operators instead of equality.

e.g.:

if (tripleAStatus == "Y") ...
Greg Ross
  • 3,479
  • 2
  • 27
  • 26
0

You are using assignments instead of equality operators in your if statements. i.e. change

if (tripleAStatus = "Y")

to

if (tripleAStatus == "Y")
Ryan Lynch
  • 7,676
  • 1
  • 24
  • 33
0

Please use ===!! this way, the type in the statement isn't casted and therefor more strictly checked if its the same type.

if (tripleAStatus === "Y") {
   discountRate = roomRate - roomRate * discountRate;
} else if (tripleAStatus === "N") {
   discountRate = roomRate;
}

/e -> & its faster!

japrescott
  • 4,736
  • 3
  • 25
  • 37
0

prompt() returns a STRING not a NUMBER.

Use parseInt() with numberOfPeople.

And the other problem, you copied and pasted the wrong variable names in your formulas. Look at how you are using discountRate instead of discountPercent.

epascarello
  • 204,599
  • 20
  • 195
  • 236