繁体   English   中英

Javascript:遍历数组

[英]Javascript: Looping through an array

这真让我抓狂! 我对javascript非常陌生(可以阅读但不能总是写出来)我的问题有三点。
用户需要输入(提示)“高个拿铁”。
我想对这个问题做一个数组,以存储a。)咖啡串和b。)咖啡价格。
2.我想使用for循环输出到目前为止订购的咖啡总量。
3.我的输出应为表格格式ex。

短单杯拿铁的价格是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]);
}

我根据您要实现的目标(您的第二个代码块)来回答这个问题。

-1 (通常由有效输出包括0的函数/方法返回)称为“前哨值”,并且在javascript中,它使用~捕获它们:所有非-1都会强制转换为true。

作为代码注释的进一步说明:

 (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> 

现在,..真正的挑战是想办法解析用户输入的字符串,确定什么是有效和完整的,什么不是(感谢您没有要求该问题的解决方案)。 我检查是否设置了sizeshots

希望这对您的学习体验有所帮助。

谢谢大家,我想我想出来了..大概知道很长的路要走...但是...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM