Home > Net >  How can I write promt value and get different answers with if else conditions?
How can I write promt value and get different answers with if else conditions?

Time:01-15

Im learning javascript and I want to practice with this question's answers.

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')

My question is how can I make automatic part yes no question and if answer 'yes' then console writes x sentence else console writes y sentence? (english not my main language so dont over think string parts. just automatic part is main question.)

thanks.

I tried

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

Im pretty sure there is a bunch of problems here but I dont know what are those.

CodePudding user response:

You're logic is correct, but in JavaScript, with the if...else statement, condition nesting is achieved using the else if clause.

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

Read more on the if...else statement on MDN, here.

Hope this helps.

CodePudding user response:

The condition in the else if syntactically wrong and also not needed:

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

CodePudding user response:

If you want, you could also use JavaScript's single line 'if' statement

You can do so with:

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

However, if you have three values like 'yes', 'no' & 'disabled', then you might want to use the traditional if else statement:

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')
}
  • Related