user8359832
user8359832

Reputation:

JavaScript temperature converter gives Invalid or unexpected token error

I was working on a JavaScript function tonight following a tutorial that would convert fahrenheit to celsius.

I tried this way:

function fahrToCels(fahrTemp) {
  return (fahrTemp − 32) * (5⁄9);
}
console.log(fahrToCels(140));

and I got the following error:

Uncaught SyntaxError: Invalid or unexpected token

I researched the correct way to write this online and found similar code to mine such as:

function toCelsius(f) {
           return (5/9) * (f-32);
      }
      document.getElementById("demo").innerHTML = toCelsius(77);

I tried running this and I did not get an error. But, I couldn't figure out why my way was wrong, so when I gave up and looked up the answer they were looking for, I found this:

function fahrToCels(fahrTemp) {
  return (fahrTemp - 32) / 1.8;
}

console.log(fahrToCels(140));

This does not give an error although it gives a different answer. So, what gives here? I have something similar above, except without the 1.8. I am unclear on what the syntax error is telling me. Pleas help.

Upvotes: 0

Views: 73

Answers (1)

You have invalid characters: and 5⁄9.

Change to:

- and 5/9.

Something like this:

function fahrToCels(fahrTemp) {
  return (fahrTemp - 32) * (5/9);
}
console.log(fahrToCels(140));

Upvotes: 1

Related Questions