-1

I'm 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.

mplungjan
  • 169,008
  • 28
  • 173
  • 236

3 Answers3

1

Your 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.

damonholden
  • 1,062
  • 4
  • 17
  • ı learned if you write else if you must finish it with last else condition. so is it wrong that I learned? Its worked btw thank you very much!! – ıLikeJavaButİtsKindaMessy Jan 14 '23 at 21:22
  • It's not a syntax error to omit the `else` clause. In practice, it really depends on your use case. Here, for example, you might want to do something **only if** `automatic` is neither `'yes'` or `'no'`. In this scenario, you could use an `else` clause. – damonholden Jan 14 '23 at 21:32
0

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')
}
Jens
  • 67,715
  • 15
  • 98
  • 113
0

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')
}
Leialoha
  • 9
  • 4
  • 1
    It is called called [Conditional_Operator or Ternary](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) but you are not using it in an appropriate manner. Instead do `console.log(automatic === 'yes' ? 'Write one' : 'Write number two')` – mplungjan Jan 17 '23 at 08:49
  • For the second example I suggest `let text = 'N/A'; if (answer === 'yes') { text = 'Text for yes'; } else if (answer === 'no') { text = 'Text for no') } console.log(text);` – mplungjan Jan 17 '23 at 08:54