# 章序号/节序号/节/笔记序号 codecademy, Variable # 概念阐释 # 举例子 # 类比、比较与对比 # 问题 [Kelvin Weather](https://www.codecademy.com/courses/introduction-to-javascript/projects/kelvin-weather-javascript) Deep in his mountain-side meteorology lab, the mad scientist Kelvin has mastered weather prediction. Recently, Kelvin began publishing his weather forecasts on his website. However there’s a problem: All of his forecasts describe the temperature in [Kelvin](https://en.wikipedia.org/wiki/Kelvin). With our knowledge of JavaScript, let’s convert Kelvin to Celsius, then to Fahrenheit. ![Kelvin, Celsius, and Fahrenheit thermometers](https://content.codecademy.com/projects/introduction-to-javascript/learn-javascript-introduction/kelvin-weather/Kelvin%20Thermometers.svg) For example, 283 K converts to 10 °C which converts to 50 °F. Mark the tasks as complete by checking them off - [x] 1. The forecast today is `293` Kelvin. To start, create a variable named `kelvin`, and set it equal to `293`. The value saved to `kelvin` will stay constant. Choose the variable type with this in mind. - [x] 2. Write a comment above that explains this line of code. - [x] 3. 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`. - [x] 4. Write a comment above that explains this line of code. - [x] 5. Use this equation to calculate Fahrenheit, then store the answer in a variable named `fahrenheit`. _Fahrenheit = Celsius * (9/5) + 32_ In the next step we will round the number saved to `fahrenheit`. Choose the variable type that allows you to change its value. - [x] 6. Write a comment above that explains this line of code. - [x] 7. When you convert from Celsius to Fahrenheit, you often get a decimal number. Use the `.floor()` method from the built-in Math object to round down the Fahrenheit temperature. Save the result to the `fahrenheit` variable. >[!hint] >- 如果数字为,39.3 >- `.ceil`等于40,趋近于最大整数 >- `.floor` 等于39,趋近于最小整数 >- 变量关键字发挥作用,改变变量,变量的最大特点就是可以变 - [x] 8. Write a comment above that explains this line of code. - [x] 9. 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`. - [x] 10. Run your program to see your results! - [x] 11. By using variables, your program should work for any Kelvin temperature — just change the value of `kelvin` and run the program again. What’s `0` Kelvin in Fahrenheit?*-273度* - [x] 12. Great work! Kelvin can now publish his forecasts in Celsius and Fahrenheit. If you’d like extra practice, try this: - Convert `celsius` to the [Newton](https://en.wikipedia.org/wiki/Newton_scale) scale using the equation below _Newton = Celsius * (33/100)_ - Round down the Newton temperature using the `.floor()` method - Use `console.log` and string interpolation to log the temperature in `newton` to the console # 问题答案 ```js const kelvin = 293; // 设定一个开尔文数 const celsius = kelvin - 273; //摄氏度等于开尔文减去273,这里可以是变量名和数字的组合 let fahrenheit = celsius * (9/5) + 32;//因为下一步需要对数字进行整数处理,所以华氏度的公式用变量关键字 fahrenheit = Math.floor(fahrenheit);//函数总是返回小于等于一个给定数字的最大整数,变量名可以等于一个方法 console.log(`The temperature is ${fahrenheit} degress Fahrenheit.`); let newton = celsius * (33/100) newton = Math.floor(newton); console.log(`The temperature is ${newton}.`) ``` # 备注(经验集/错误集) [codecademy答案](https://www.codecademy.com/courses/introduction-to-javascript/projects/kelvin-weather-javascript) - 在数学公式中,变量可以赋值为「另一个变量与数字」的写法,即公式; - 也可以赋值为一个「方法」(*之前在学方法时都是用在console里*)