Problem with prompt task in Javascript lesson


#1

This is the task given to me

1.The forecast today is 294 Kelvin. To start, create a variable named kelvin, and set it equal to 294.
We will not change the value saved to kelvin. Choose the variable type with this in mind.

2.Celsius is similar to Kelvin — the only difference is that Celsius is 273 degrees less than Kelvin.
Let’s convert Kelvin to Celsius by subtracting 273 from the kelvin variable. Store the result in another variable, named celsius.

3.Use this equation to calculate Fahrenheit, then store the answer in a variable named fahrenheit.
In the next step we will round the number saved to fahrenheit. Choose the variable type that allows you to change its value.

4.When you convert from Celsius to Fahrenheit, you often get a decimal number.
Use the .floor() method from the Math library to round the Fahrenheit temperature. Save the result to the fahrenheit variable.

5.Use console.log and string interpolation to log the temperature in fahrenheit to the console as follows: The temperature is TEMPERATURE degrees fahrenheit.
Use string interpolation to replace TEMPERATURE with the value saved to fahrenheit.

6.You can make the Kelvin Weather project more interactive by asking the user what the current Kelvin temperature is through a prompt pop-up window.

prompt is a JavaScript function that will generate a pop-up window that asks the user for input, then it will assign their input to a variable.

Make the kelvin variable equal to this code:
const kelvin = prompt(‘What is the Kelvin temperature today?’);

Below is the code i have written, but i get a notification of “SyntaxError: Missing initializer in const declaration”

const kelvin = 294;//I choosed const variable because theis variable is not going to change it’s value

const Celsius ;
var Celsius = kelvin - 273;

let fahrenheit;
var fahrenheit = Celsius * (9/5) + 32;
// I wrote the formula of converting from fahrenheit to Celsius in order to set the fahrenheit varaible in it’s value

fahrenheit = Math.floor(fahrenheit);

console.log(The temperature is ${fahrenheitE} degrees fahrenheit);

const kelvin = prompt(‘What is the Kelvin temperature today?’);


#3

Hi @asicode,

From the cursory look, you have this code:

The usage of const described in MDN documentation as: The value of a constant cannot change through re-assignment, and it can’t be redeclared.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

So when you have const Celsius without assigned value (means undefined), you can’t have Celsius redeclared in var

same goes for const kelvin, you can’t have it at the beginning, then redeclare it at the end of your code.

As for the error, it shows up because there is no value specified during declaration of const Celsius in your code. You can read more in the documentation below:
SyntaxError: Missing initializer in const declaration (Chrome)


Some Notation and Hint:

  • Spelling error at ${fahrenheitE}
  • It is a great practice to maintain a consistent capitalization of variable names, you have (kelvin vs Celsius vs fahrenheit)
  • the placement of prompt in the end will not serve its purpose as JavaScript code is read from top to bottom. Value saved through prompt in the end will not be used for any operation in the code. Run your code few times with different value in the prompt input you will notice that.
  • I’m confuse with the need of let fahrenheit and then var fahrenheit in your code, why the need of redeclaration such coding structure?
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
  • It will be great if you can paste the link to the lesson for others to refer, format the code according by referring here if you have future questions.

Cheers :slight_smile:


#5