当前位置:  开发笔记 > 编程语言 > 正文

使用JS的数组问题

如何解决《使用JS的数组问题》经验,为你挑选了1个好方法。

问题是重复的,抱歉.



1> davidkonrad..:

您的嵌套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()将索引作为数字返回,而不是作为数组.

推荐阅读
农大军乐团_697
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有