-5

I got an unexptected identifier error on this line:

If userInput <= 7

I really am new at this and I don't understand what that means. I'm new to javascript but I'm learning. I don't know what else to say as my knowledge of programming isn't great at all.

<html>
<body>
<script type="text/javascript">

   // Declare Constants and Variables
   var stateTax;
   var userInput;
   var finalTax;
   var BR = "</br >"; // Line break
   var ES = " ";  // Empty string
   // Welcome user 
   document.write("Welcome to Sales Tax Calculator" + BR);
   document.write("This program calculates sales tax");
   document.write("under $1.00");
   // take user input
   userInput = Prompt("Please enter a number(Cents)" + ES);

   // call selection structure
   If userInput <= 7 then
   stateTax = 0; 

   else if userInput <= 21 then
   stateTax = 1;

   else if userInput <= 35 then
   stateTax = 2 ;

   else if userInput <= 49 then
   stateTax = 3; 

   else if userInput <= 64 then
   stateTax = 4;

   else if userInput <= 78 then
   stateTax = 5;

   else if userInput <= 92 then
   stateTax = 6;

   else if userInput <= 99 then 
   stateTax = 7;

   else if userInput > 99 then
   document.write("Error, Please enter a value less than 99!");
   end if

   // Calculate and Display sales tax
   finalTax = userInput * stateTax;

   document.write("Sales tax equals: " + finalTax);
   document.write("Thank you for using tax calculator");
</script>
</body>
</html>
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Tou Saine
  • 3
  • 3
  • You're missing brackets around the conditional statements. See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else –  Jun 27 '15 at 00:23
  • You have a capital If. Also If construct is not correct. http://stackoverflow.com/questions/4005614/elseif-syntax-in-javascript – aadarshsg Jun 27 '15 at 00:23
  • 3
    This is barely JavaScript. There are many, many errors in this script. You need to study the language properly: [JavaScript Guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Introduction) – Oka Jun 27 '15 at 00:28
  • var BR = ""; =
    , something wrong with the syntax...
    – Proxytype Jun 27 '15 at 00:29

1 Answers1

0

There's a couple of syntax issues in your code. One thing you can do to help find such problems is go to jshint.com and paste your code into it. It will then show you many common errors. Some of the errors might be confusing, but I think overall it will help more than hurt. Alternatively, use an editor that has javascript linting so that it hilites potential errors.

Some of the specific syntax errors in your code:

  • the "p" in Prompt() must be lowercased (javascript is case-sensitive)
  • the "i" in If must be lowercased.
  • you must put the conditional after the if keyword in parenthesis (e.g., if (userInput <= 7) {)
  • you cannot use then to mark the beginning of a block of code. Instead use use "{". Also use "}" to mark the end of the block of code instead of keywords like endif

As an example, this block of code

If userInput <= 7 then
stateTax = 0; 
else if userInput <= 21 then

should look like this

if (userInput <= 7) {
    stateTax = 0;
} else if (userInput <= 21) {

As a side note, the brackets are optional if there is only one statement within the conditional block, but I strongly recommend always including brackets to avoid problems when you are later maintaining the code. Also, you can use whatever indentation you want, I just used 4 spaces because that's what I've found to be the most common style in open-source code.

Ed Ballot
  • 3,405
  • 1
  • 17
  • 24