gpt4 book ai didi

JavaScript 多转换器

转载 作者:行者123 更新时间:2023-11-28 03:37:59 26 4
gpt4 key购买 nike

我在 javascript 中有这 3 个函数(多转换器),但我不明白为什么我不能让程序显示结果。

function WhichConversion () {
var answer = window.prompt( " Welcome to the Mutliconverter! What do you want to convert? ( Dollars, Francs, Knots");
if (answer == "Dollars", "Knots", "Francs"){
return true;
} else {
return false;
}
}
function AskForValue () {
var answer = window.prompt( " Enter a Value to Convert")
return answer;
}
while (WhichConversion()) {
var ConversionType = WhichConversion();
var ConversionValue = AskForValue();
var resultMessage = " The result is ";
var result = 0;

if (ConversionType == "Dollars"){
resultMessage += DollarstoEuros(Number(ConversionValue))
.toString();
}else if (ConversionType == "Francs"){
resultMessage += SwissFrancsToEuros(Number(ConversionValue))
.toString();
}else if (ConversionType == "Knots"){
resultMessage += KnotstoKph(Number(ConversionValue))
.toString();
}else{
resultMessage = " Pay attention! That conversion is not supported.";
}
}
console.log(resultMessage);

最佳答案

用于检查要使用哪种转换的 if 语句只有一项检查 answer == "Dollars"。要使脚本适用于其他值,您需要创建多个检查:

if (答案 == "美元"|| 答案 == "纽特"|| 答案 == "法郎"){

有关工作示例,请检查以下代码:

var currentCurrency = '';

function WhichConversion () {
var answer = window.prompt( " Welcome to the Mutliconverter! What do you want to convert? ( Dollars, Francs, Knots");

currentCurrency = answer;

return answer !== null
}
function DollarstoEuros(value){
return value * 12;
}

function AskForValue () {
var answer = window.prompt( " Enter a Value to Convert")
return answer;
}

while (WhichConversion()) {
var ConversionValue = AskForValue();
var result = 0;
var resultMessage = " The result is ";


if (currentCurrency == "Dollars"){
resultMessage += DollarstoEuros(Number(ConversionValue))
.toString();
}else if (currentCurrency == "Francs"){
resultMessage += SwissFrancsToEuros(Number(ConversionValue))
.toString();
}else if (currentCurrency == "Knots"){
resultMessage += KnotstoKph(Number(ConversionValue))
.toString();
}else{
resultMessage = " Pay attention! That conversion is not supported.";
}

console.log(resultMessage);
}

关于JavaScript 多转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57543541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com