Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I originally had it figure in the tip (20% of bill) and tax (0.06) into the total of the meal and then confirm how much it would cost. Today I wanted to make it calculate the tax, and have the tip be optional (if at a fast food restaurant.) I used an if/ else if statement to do this, but it seems to display the tip no matter what. Any help would be appreciated, but please try and explain it simply as I am relatively new to coding; which probably means it is a simple mistake. The code is below. Thank you.

JavaScript
var meal = prompt("How much was your meal?")

var tax = 0.06
var tip = 0.20

var withTax = meal +++ meal * tax
confirm("Your total is" + " " + withTax)
  //problem starts here and ends at the else if
var yesNo = prompt("Would you like to include a tip?")
  //for some reason it always displays tip and won't run the else if
if (yesNo = "yes") {
  var total = withTax +++ withTax * tip

  confirm("Your total(plus tip) is" + " " + total)
}
//code below doesn't run?
else if (yesNo = "no") {

  confirm("Okay then, just" + " " + withTax)
}


What I have tried:

I have tried moving the if and else if around, and have looked it over several times. I am running the code in a website called https://jsfiddle.net/ if that helps.
Posted
Updated 5-Mar-16 11:14am
v3

1 solution

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Why are you using the operator +++ ? and what should it do (for you) ?
To do comparison, use == intead of = which is for assignation.
JavaScript Tutorial[^]

More simplifications should be applied.
 
Share this answer
 
v3
Comments
Patrice T 5-Mar-16 18:11pm    
If question solved, use button to close the question or say ot is answered.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900