Javascript 意外标记}。Celcius至Farenheit变流器
在我开始提问之前,我只想说一件事。我是一个对编程感兴趣的年轻人,所以如果答案非常简单,请不要对我生气。对不起,我的英语不好 事情是这样的。我试图用JavaScript在华氏温度和摄氏温度之间进行转换。我根本无法得到我在这里输入的代码,我做错了。我的代码Javascript 意外标记}。Celcius至Farenheit变流器,javascript,Javascript,在我开始提问之前,我只想说一件事。我是一个对编程感兴趣的年轻人,所以如果答案非常简单,请不要对我生气。对不起,我的英语不好 事情是这样的。我试图用JavaScript在华氏温度和摄氏温度之间进行转换。我根本无法得到我在这里输入的代码,我做错了。我的代码 //Line 2 asks the user what he/she want to convert. Either celcius to farenheit, or farenheit to celcius var userChoice = p
//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!")
}
};
请告诉我我做错了什么,而不仅仅是粘贴正确的代码!:) if(userChoice==“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!");
}
};
更仔细地构造代码,您将立即发现错误。用法:将在js中帮助您!不要为自己是新手而道歉。每个人都是新人,至少一次。我们中的许多人都是新手(不断学习)