在谈到这个问题之前,我只想说一件事。我是一个对编程感兴趣的年轻人,所以如果答案非常简单,请不要愤怒。 PS。抱歉我的英语不好:P
这就是事情。我试图用JavaScript在Fahrenheit和Celsius之间制作这个转换器。我只是无法得到我enter code here
做错的事。我的代码;
//Line 2 asks the user what he/she want to convert. Either celcius to farenheit, or farenheit to celcius
var userChoice = prompt("For converting farenheit to celcius, type FtoC. For converting celcius to farenheit, type CtoF");
//Defines a function to calculate the math
var celciustofarenheit = function (userChoice) {
if (userChoice === "CtoF")
var CtoFchoice = prompt("Whats the degree you want to convert?")
var result1 = CtoFchoice * 9 / 5 + 32;
console.log(CtoFchoice, "is", result1, "in farenheit!")
}
if (userChoice === "FtoC") {
var FtoCchoice = prompt("Whats the number you want to convert?")
var result2 = FtoCchoice - 32 * 5 / 9;
console.log(FtoCchoice, "is", result2, "in celcius!")
}
};
请告诉我我做错了什么,而不是只是粘贴正确的代码! :)
答案 0 :(得分:2)
if (userChoice === "CtoF")
后你缺少大括号。如果你只是正确地缩进代码,你会直接发现这样的错误。
答案 1 :(得分:0)
更正代码
//Defines a function to calculate the math
var celciustofarenheit = function (userChoice) {
if (userChoice === "CtoF") {
var CtoFchoice = prompt("Whats the degree you want to convert?");
var result1 = CtoFchoice * 9 / 5 + 32;
console.log(CtoFchoice, "is", result1, "in farenheit!");
}
if (userChoice === "FtoC") {
var FtoCchoice = prompt("Whats the number you want to convert?");
var result2 = FtoCchoice - 32 * 5 / 9;
console.log(FtoCchoice, "is", result2, "in celcius!");
}
};