问题是重复的,抱歉.
您的嵌套if
将永远不会执行,index
不能同时为1和3.我建议你使用一个switch
,它基本上是你想要做的:
var productPurchase = readlineSync.question('Would you like to purchase a product? '); if (productPurchase == "yes") { index = readlineSync.keyInSelect(products, 'What product would you like?'); switch (index) { case 1 : console.log('Thank you, your Drink' + ' has now been dispensed.'); break; case 2 : console.log('Thank you, your Crisps' + ' has now been dispensed.'); break; case 3 : console.log('Thank you, your Chocolate' + ' has now been dispensed.'); break; case 4 : console.log('Thank you, your Candy' + ' has now been dispensed.'); break; default : console.log('something went wrong'); break; } }
注意:纠正了错误的用法index
.keyInSelect()
将索引作为数字返回,而不是作为数组.