如何在不使用对象和switch语句的情况下使这个javascript更具说服力?

如何在不使用对象和switch语句的情况下使这个javascript更具说服力?,javascript,arrays,function,variables,if-statement,Javascript,Arrays,Function,Variables,If Statement,我不想打印出所有可能的价格组合,但我已经搜索了很长时间,找不到更好的方法: 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', 'Ta

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

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的东西,在这里您需要转换为整数。以下是文档

您可以这样做:

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);
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' ]
                ]
不过,您确实需要了解对象。也要调查一下。。JS-documentation中的in函数将比我更好地解释它,但简而言之,您可以通过for..in-循环父数组的属性,基本上是通过数组列表。与使用相同的思想相比,您可以部分地循环遍历每个数组。为了给您一个直观的表示,您可以使用如下数组:

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

物体使它变得美丽。为什么不想对数组数据使用一个简单的基于JSON的结构呢?我需要在没有它们的情况下使用它。只有语句、函数、变量和数组。这听起来像是一个家庭作业助手……神圣堆栈溢出上不允许这样做吗?我不管这是不是家庭作业,对我来说都无关紧要。但这就是为什么不能使用对象的原因吗?我尝试了一段时间实现它,但它不起作用。但我想我会继续努力的。谢谢。它只是把这两个数字放在一起,而不是加在一起。变量定义前的A+与parseInt的作用相同,但更短。因此,它不是x=parseIntprompt,而是x=+prompt;我不知道!谢谢你的提示,@Brendan。没问题。无论是谁制作了javascript,他决定给+号3或4件事情,我不明白,但它很方便。请使用正确的循环。顺便说一句,您的控制台.log'Error'似乎放错了位置,它在内部循环中没有多大意义。@Bergi我为他的特定需求使用的特定示例是完全有效的,并且优于array.length-尽管我同意它不适合所有情况。console.log也放置在需要的位置。下次在你评论之前检查一下代码。我必须承认我没有仔细看你的代码。我又做了一次,只发现了更多的错误——如果可以的话,我会再次投反对票。下次在回答之前,请先试用您的代码。提示:它当前只记录错误21次,而不考虑input@Bergi啊,你说得很对。我忘记了那个物体本身。不过,在数组问题上的迭代仍然是错误的。