Expected an Identifer and instead saw 'else'. What should I do?

homework
javascript

#1

Problem
The program won’t run, it says “Expected an Identifer and instead saw ‘else’.” I’ve tried everything I know - what should be done?

About the program
I’m creating a program where it should prompt the user for the number of pounds to be converted. The program should check if the number of pounds is greater than 0 and a number. If the input is 0 or a negative number or the input is not a number then an error message should be displayed – “input error, please input a number greater than 0”.
If the pounds to be exchanged is > £500 then no commission is charged. If the number of pounds worked is < = £500 then 3% commission is charged.
An output message should be displayed to show the number of euros exchanged.

Program

//Set variable
const exchange = 1.19;
var pounds = 0;
var euros = 0.0;

//Using prompt to get value from user
pounds = prompt("Enter the amount of pounds you wish to convert:");
pounds = parseInt(pounds);

//Display amount of euros depending on how much pounds user enters
if (pounds > 0)
  {
    if (pounds > 500)
      euros = (pounds * exchange).toFixed(2);
    alert('€' + euros);
  } else {
    euros = (pounds * exchange);
    euros = (euros - ((euros / 100) * 3)).toFixed(2);
    alert('€' + euros);
  }
else;
{
alert("Input error! Please input a number greater than 0!");
}

As an amateur js programmer, any other tips/advice would be much appreciated! :slight_smile:


#2

There are no semi-colons in an if statement.

if () {

}
else if () {

} else {

}

#3
//Set variable
const exchange = 1.19;
var pounds = 0;
var euros = 0.0;

//Using prompt to get value from user
pounds = prompt("Enter the amount of pounds you wish to convert:");
pounds = parseInt(pounds);

//Display amount of euros depending on how much pounds user enters
if (pounds > 0)
  {
    if (pounds > 500)
      euros = (pounds * exchange).toFixed(2);
    alert('€' + euros);
  } else {
    euros = (pounds * exchange);
    euros = (euros - ((euros / 100) * 3)).toFixed(2);
    alert('€' + euros);
  }
else
{
alert("Input error! Please input a number greater than 0!");
}

I amended it, the program still doesn’t work - also, I’m not using else if statement - this is my design, if it helps.

Begin
get pounds
If pounds > 0
	If pounds > 500
		set  euros to pounds * exchange
		display euros
	else
		set  euros to (pounds * exchange) 
		set euros to (euros – ((euros/100)*3))
		display euros
else
	display invalid entry error message
end if
end	


#4

It seems odd that there would be no commission for amounts larger than 500. I would interpret that to mean that commisssion is charged on only the first 500 pounds.

euros = (pounds - 500) * exchange + 500 * exchange * 0.97

euros = exchange * ((pounds - 500) + 500 * 0.97)

Here’s some trial code…

let exchange = 1.19;
let pounds = 1500;    //=> £1500 => €1767.15
//pounds = 500;       //=> £500  => €577.15
//pounds = 100;       //=> £100  => €115.43
if (pounds > 500) {
   console.log(`£${pounds} => €${+(exchange * ((pounds - 500) + 500 * 0.97)).toFixed(2)}`);
} else if (pounds > 0) {
   console.log(`£${pounds} => €${+(exchange * pounds * 0.97).toFixed(2)}`);
} else {
   console.log("Input error.")
}

https://repl.it/Mj66


#5

Any chance you could help me with the issue i have - “Expected an Indentifier and instead saw ‘else’.”?


#6

Hi @ibobbyo,

I have no comment/idea about how your code is structured, but I definitely can help spot the syntax issue.

See the comments below:

//Display amount of euros depending on how much pounds user enters
if (pounds > 0)
{
    if (pounds > 500)  //<=== missing an opening curly bracket for second if
      euros = (pounds * exchange).toFixed(2);
    alert('€' + euros);
  } else {
    euros = (pounds * exchange);
    euros = (euros - ((euros / 100) * 3)).toFixed(2);
    alert('€' + euros);
  }  // <=== closes else
//<=== missing another closing curly bracket for first if
else
{
alert("Input error! Please input a number greater than 0!");
}

Add those curly brackets, the code should work.

Cheers :slight_smile:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.