Javascript:循环遍历数组

时间:2014-11-22 00:18:19

标签: javascript arrays for-loop

这让我抓狂!我对javascript很新(可以读它但不总是写它)我的问题是三重的。
用户需要输入(提示)"高单拿铁"。
1.我想为这个问题添加一个数组来存储咖啡串和咖啡价格。(b)咖啡价格 2.我想使用for循环输出到目前为止订购的咖啡总量 3.我的输出应采用表格格式例如

短单拿铁的价格是R10
双高咖啡的价格是R15

var coffee = [ ];
var price = [ ];

for (var i = 0; i < 2; i++) {

var coffee = prompt("What coffee do you want:", "");

// Size
if (coffee.indexOf('short') > -1) {
    var size = 7;
}
if (coffee.indexOf('tall') > -1) {
    var size = 9;
}
if (coffee.indexOf('grande') > -1) {
    var size = 11;
} 

// Shots
if (coffee.indexOf('single') > -1) {
    var shots = 1;
}
if (coffee.indexOf('double') > -1) {
    var shots = 2;
}
if (coffee.indexOf('triple') > -1) {
    var shots = 3;
}

// Is cappuccino?
if (coffee.indexOf('cappuccino') > -1) {
    var extra = 2;
} else {
    var extra = 0;
}

var price = (size + (3 * shots) + extra);    
console.log(coffee + "'s price is R" + price);  
}

我想要实现的一个例子:

var coffee = [ ];
var price = [ ];

coffee.push("short single latte");
price.push(10);

coffee.push("double tall latte");
price.push(15);

var i;
for (i = 0; i < coffee.length ; i++)
{
    console.log(coffee[i] + "'s price is R" + price[i]);
}

2 个答案:

答案 0 :(得分:0)

我根据您想要实现的内容(第二个代码块)来确定答案。

-1(通常由有效输出包含0的函数/方法返回)称为'sentinel value',在javascript中,使用~捕获它们:不是-1的所有内容将强迫真实。

进一步解释为代码中的注释:

(window.Orders=function(){    // our constructor function
   this.clr();              // init by calling clr.
}).prototype={              // set inherited methods
   clr: function(){         // clear
           this.coffee=[];  // empty array of unknown length
           this.price=[];   // empty array of unknown length
        }
,  add: function(){         // build orders list
           var inp, size, shots, extra;
           while((inp=prompt('What coffee do you want:')) !== null){
             size=0;        // Size
             if(      ~inp.indexOf('short' ) ) size=  7;
             else if( ~inp.indexOf('tall'  ) ) size=  9;
             else if( ~inp.indexOf('grande') ) size= 11;
             shots=0;       // Shots
             if(      ~inp.indexOf('single') ) shots= 1;
             else if( ~inp.indexOf('double') ) shots= 2;
             else if( ~inp.indexOf('triple') ) shots= 3;
             extra= ~inp.indexOf('cappuccino') ? 2 : 0; //cappuccino?
             if( size && shots ){   //abuse price to check input
                this.coffee.push(inp);
                this.price.push(size + 3 * shots + extra);
             } else alert('please enter valid order');
           }
        }
,  get: function(EOL){ //output orders
           var i=0, L=this.coffee.length, r=new Array(L);
           for(; i<L; i++){  //using a for loop as you requested.
             r[i]=this.coffee[i] + "'s price is R" + this.price[i];
           }
           return r.join(EOL || '<br>');  //return string using arg EOL or '<br>'
        }
};
<!-- HTML AND *EXAMPLE* usage -->
<button onclick="
   var orders=new Orders();   // Construct new var orders
   orders.add();              // Start filling it
   document.getElementById('out').innerHTML=orders.get(); //get output
   //orders.clr()  //clears orders if you want to reuse it without spawning a new
">get orders (cancel/escape to quit)</button>
<br>
Output: <div id="out"></div>

现在......真正的挑战在于思考如何解析用户输入字符串,确定什么是有效的&amp;完成什么不是(谢天谢地你没有要求解决这个问题)。我检查了sizeshots是否已设置。

希望这有助于您的学习体验。

答案 1 :(得分:0)

谢谢你们,我想我已经明白了......知道它可能还有很长的路要走......但是......

var coffeeName = new Array();
var priceSingle = new Array();

// Loop 2 times
for ( var i = 0; i < 2; i++){

// Prompt Coffee
coffee = prompt("What coffee do you want:", "");

// Size
if (coffee.indexOf('short') > -1) {
    var size = 7;
}
if (coffee.indexOf('tall') > -1) {
    var size = 9;
}
if (coffee.indexOf('grande') > -1) {
    var size = 11;
} 

// Shots
if (coffee.indexOf('single') > -1) {
    var shots = 1;
}
if (coffee.indexOf('double') > -1) {
    var shots = 2;
}
if (coffee.indexOf('triple') > -1) {
    var shots = 3;
}

// Is cappuccino?
if (coffee.indexOf('cappuccino') > -1) {
    var extra = 2;
} else {
    var extra = 0;
}

// Work out Price
var price = (size + (3 * shots) + extra);

// Push coffee to coffeeNameArray
coffeeName.push(coffee);

// Push price to priceSingleArray
priceSingle.push(price);

}

// Loop coffeeName length - Output List
for (var i = 0; i < coffeeName.length; i++)
{
    console.log(coffeeName[i]+"'s price is R"+priceSingle[i]);
}