-2

I have multiple errors in my code. When I run the code, it gives an error saying that there is an unexpected identifier. Here is the full error, "SyntaxError: Unexpected identifier at /script.js:46:16". However, when I check through lines 46 through 16, I cant find any unclosed functions or methods. When I comment the if statement on line 46, it just gives an error on the other if statement. Can someone help me?

Heres the code:

function print(str){
    console.log(str)
}

function farhToKelvin(far){
    const cel = Math.floor(far / (9/5) - 32)
    const kel = cel + 273
    return kel
}

function farhToCelsius(far){
    const cel = Math.floor(far / (9/5) - 32)
    return cel
}

function convertToFarh(type, val){
    type.toLowerCase()
    val.toLowerCase()
    if (type == "kelvin"){
        return Math.floor(far / (9/5) - 32 - 273)
    }
    else if(type == "celsius"){
        return Math.floor(far / (9/5) - 32)
    }
}


while (farh != "exit"){
    var farh = prompt("enter a farhanhite tempature: ")
    var type = prompt("convert it to celsius, or kelvin")
    type.toLowerCase()
    if (type == "celsius"){
        const c = farhToCelsius(farh)
        var val = convertToFarh(c)
        if (val > 50 or val == 50){
            print("it is cold, you should wear a coat")
        }
        if (val > 50 or val == 50){
            print("it is hot, you should wear a tank top")
        }
    }
    else if(type == "kelvin"){
        const k = farhToKelvin(farh)
        var val = convertToFarh(k)
        if (val > 50 or val == 50){
            print("it is cold, you should wear a coat")
        }
        if (val > 50 or val == 50){
            print("it is hot, you should wear a tank top")
        }
    }
}
phentnil
  • 2,195
  • 2
  • 14
  • 22
coder lol
  • 49
  • 6
  • 1
    I believe `script.js:46:16` means file script.js, line 46, column 16. So which line is 46? – 001 Nov 30 '21 at 01:02
  • 2
    `if (val > 50 or val == 50){` I don't think `or` is a valid alias for `||` (logical OR). – 001 Nov 30 '21 at 01:04
  • 2
    Also, `type.toLowerCase()` does not modify the original string so this line basically does nothing. Use `type = type.toLowerCase()` – 001 Nov 30 '21 at 01:06

3 Answers3

2
    if (val > 50 or val == 50){

In Javascript, instead of or, we use ||. If you have a similar problem again, you might want to take a look at What does this symbol mean in JavaScript?

user1280483
  • 470
  • 4
  • 11
1

change

if (val > 50 or val == 50) 

to this

if (val > 50 || val == 50) 

or better to this

if (val >= 50) 

there are similar problems on line 50 and 60 and 64

you need to update them all. logically line 46 and 50 are the same. based on you print message the line 50 should be if (val > 50 ) but line 45 should be if ( val <= 50)

so you have both syntax and semantic problems in your code to address

pref
  • 1,651
  • 14
  • 24
0

Here's a complete list of the issues you have in your code:

  • Using or instead of || (as others have stated)
  • Using incorrect variable name in the convertToFarh function
  • Omitting type parameter when calling convertToFarh
  • Attempting to call String.toLowerCase() on a number variable
  • Misspelling fahrenheit

Using or instead of || (Logical OR)

In your code, I believe you are intending to indicate if (val > 50 || val == 50) { instead of using or as used in other programming languages.

Before:

        if (val > 50 or val == 50){
            print("it is cold, you should wear a coat")
        }
        if (val > 50 or val == 50){
            print("it is hot, you should wear a tank top")
        }

After:

        if (val > 50 || val == 50){
            print("it is cold, you should wear a coat")
        }
        if (val > 50 || val == 50){
            print("it is hot, you should wear a tank top")
        }

This logic also doesn't make sense. Perhaps you mean value < 50 in the first one and val >= 50 in the second?

You also repeat yourself whether you're converting to/from Kelvin or Celsius, so that code could be extracted out into its own function or just reduce the if..else blocks down to only affect the necessary variables and perform the comparison after these blocks.

Using incorrect variable name in convertToFarh function

In the convertToFarh function, you reference a variable named far, but there's no variable by that name. So you either mean to reference the val argument or you are trying to reference the fahr variable declared outside the function. In my code, I assume the former is the case val and rename it as follows:

function convertToFarh(type, val){
    type.toLowerCase()
    val.toLowerCase()
    if (type == "kelvin"){
        return Math.floor(val / (9/5) - 32 - 273)
    }
    else if(type == "celsius"){
        return Math.floor(val / (9/5) - 32)
    }
}

Omitting type parameter when calling convertToFarh

In both function calls to convertToFarh, you use the c or k variable as the value of the val parameter, but you don't indicate the type. I have fixed this to indicate the type for each part:

var val = convertToFarh("celsius", c);
var val = convertToFarh("kelvin", k);

Attempting to call String.toLowerCase() on a number variable

In the convertToFarh function, you are attempting to call the String.toLowerCase() method on a number type (val) which gives an error. In my code, I simply commented this out and confirmed it can safely be removed.

Misspelling fahrenheit

It might not seem like a big deal, but making sure variables have proper spelling helps when others are reviewing your code (whether bug-fixing or general code review). I have fixed function names, variable names, and any references to fahrenheit in your code to be the proper spelling. This includes:

  • "enter a fahrenheit temperature: "
  • function fahrToKelvin and function calls
  • function fahrToCelsius and function calls
  • function convertToFahr and function calls
  • The variable named farh to fahr
  • Function parameters named far were changed to val to avoid variable name collision

Full code

function print(str) {
  console.log(str);
}

function fahrToKelvin(val) {
  const cel = (val - 32) / (9 / 5);
  return Math.floor(cel + 273.15);
}

function fahrToCelsius(val) {
  const cel = Math.floor((val - 32) * 5 / 9);
  return cel;
}

function convertToFahr(type, val) {
  if (type == "kelvin") {
    return Math.floor(val / (5 / 9) - 459.67);
  } else if (type == "celsius") {
    return Math.floor(val / (9 / 5) + 32);
  }
}

var fahr = prompt("enter a fahrenheit tempature: ");
var type = prompt("convert it to celsius, or kelvin");
type = type.toLowerCase();
if (type == "celsius") {
  const c = fahrToCelsius(fahr);
  var val = convertToFahr("celsius", c);
  if (val < 50) {
    print("it is cold, you should wear a coat");
  }
  if (val >= 50) {
    print("it is hot, you should wear a tank top");
  }
} else if (type == "kelvin") {
  const k = fahrToKelvin(fahr);
  var val = convertToFahr("kelvin", k);
  if (val < 50) {
    print("it is cold, you should wear a coat");
  }
  if (val >= 50) {
    print("it is hot, you should wear a tank top");
  }
}
phentnil
  • 2,195
  • 2
  • 14
  • 22