我如何写提示值并得到不同的答案与if else条件?



我正在学习javascript,我想练习这个问题的答案。

var brand = prompt('Car brand?')
var model = prompt('Car model?')
var tank = prompt('Aracin yakit deposu ne kadar?')
var fuelPrice = 7.60
var fuelPriceTotal = (tank * fuelPrice)
var automatic = prompt('Otomatik mi?')
console.log(brand + ' ' + model + ' ' + tank + ' ' + 'litre yakit deposuna sahip toplam yakit fulleme fiyati' + ' ' +
parseInt(fuelPriceTotal) + 'TL' + ' ' + 'Araç' + ' ' + automatic + 'tir')

我的问题是我如何使自动部分是没有问题,如果回答"是",那么控制台写x句,否则控制台写y句?英语不是我的主要语言,所以不要过多考虑弦乐部分。只是自动部分是主要问题。)

谢谢。

我试着

if (automatic === 'yes') {
console.log('Write one')
} else (automatic === 'no'){
console.log('write number two')
}

我很确定这里有一堆问题,但我不知道是什么问题。

你的逻辑是正确的,但是在JavaScript中,使用if...else语句,条件嵌套是使用else if子句实现的。

if (automatic === 'yes') {
console.log('Write one')
} else if (automatic === 'no'){ // You were missing the `if` here.
console.log('write number two')
}

在MDN上阅读更多关于if...else声明的信息,在这里。

希望对你有帮助。

else语句中的条件语法错误,也不需要:

if (automatic === 'yes') {
console.log('Write one')
} else {
console.log('write number two')
}

如果你愿意,你也可以使用JavaScript的单行' If '语句

您可以使用:

(automatic === 'yes') ? console.log('Write one') : console.log('Write number two')

但是,如果您有三个值,如'yes', 'no' &'disabled',那么你可能需要使用传统的if else语句:

if (automatic === 'yes') {
console.log('Write one')
} else if (automatic === 'no'){
console.log('write number two')
} else if (automatic === 'disabled'){
console.log('write number three')
}

最新更新