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!
//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
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.
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!");
}