如何在不使用对象和switch语句的情况下使这个javascript更加雄辩

How do I make this javascript more eloquent without using objects and switch statements?

本文关键字:情况下 javascript 语句 对象 switch      更新时间:2023-09-26

我不想键入所有可能的价格组合,但我已经搜索了很长时间,找不到更好的方法:

var selection = new Array (4);
selection[0] = new Array ('$210' 'etc', 'etc', 'etc', 'etc');
selection[1] = new Array ('Solar', 'Supernova', 'Quasar', 'Galaxy', 'Blackhole');
selection[2] = new Array ('Talk', 'Talk & Text', 'Talk, Text & Data');
selection[3] = new Array ('One Year', 'One', 'Two Years', 'Two', 'Three Years', 'Three', 'Four Years', 'Four');

function selectPhone () {
    var yourPhone = prompt("What kind of Smartphone would you like: Solar: $100, Supernova: $200, Quasar: $300, Galaxy: $400, Blackhole: $500?");
    if (yourPhone == selection[1][0]) {
        console.log("You picked: " + yourPhone + "."), selectPlan ();
    } else {
        console.log("Error.");
    }
}

function selectPlan () {
    var yourPlan = prompt("What Plan Would You Like: Talk: $10, Talk & Text: $20 or Talk, Text & Data: $30?");
    if (yourPlan == selection[2][0]) {
        console.log("You picked: " + yourPlan + "."), selectTerm ();
    } else {
        console.log("Error.");
    }
}

function selectTerm () {
    var yourTerm = prompt("What Term Would You Like: One Year: $100, Two Years: $200, Three Years: $300 or Four Years: $400?");
    if (yourTerm == selection[3][0] || selection [3][1]) {
        console.log("You picked: " + selection[3][0] + ". 'n Your total is: " + selection[0][0]);
    } else {
        console.log("Error.");
    }
}

selectPhone ();

我不知道如何对它进行编程,这样它就可以提取所做的选择,并将其转换为数值,然后对其进行简单的加法运算。我是一个初学者,所以请解释一切。非常感谢!!

您可以使用类似parseInt()的东西来转换为整数。这是文件https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

你这样做:

var selection = new Array (4);
selection[0] = new Array ('$210', 'etc', 'etc', 'etc', 'etc');
selection[1] = new Array ('Solar', 'Supernova', 'Quasar', 'Galaxy', 'Blackhole');
selection[2] = new Array ('Talk', 'Talk & Text', 'Talk, Text & Data');
selection[3] = new Array ('One Year', 'One', 'Two Years', 'Two', 'Three Years', 'Three', 'Four Years', 'Four');

function selectPhone (selectionArray) {
    var yourPhone = prompt("What kind of Smartphone would you like: Solar?");
    for( s1 in selectionArray ){
        for( s2 in selectionArray[s1] ) {
            if (yourPhone == selectionArray[s1][s2] ) {
                console.log("You picked: " + yourPhone + ".");
            } else {
                console.log("Error.");
            }
        }
    }
}
selectPhone(selection);

不过,您确实需要了解对象。另外,看看JS中的for .. in函数,文档会比我更好地解释它,但简而言之,您可以循环遍历父数组的属性(使用for.in),基本上是遍历数组列表。与使用相同的想法相比,您可以部分地循环遍历每个数组。为了给你一个视觉表示,你的数组看起来像这样:

var selection  = [//outer array
                    [ //inner array - property of outer array (first for..in) = s1
                       '$210', //property of inner array (second for..in) = s2                          
                       'etc'
                    ], 
                    [ 'Solar', 'Supernova', 'Quasar', 'Galaxy', 'Blackhole' ], 
                    [ 'Talk', 'Talk & Text', 'Talk, Text & Data' ],
                    [ 'One Year', 'One', 'Two Years', 'Two', 'Three Years', 'Three', 'Four' ]
                ]
相关文章: