2014-11-22 135 views
1

这让我疯狂!我很新的JavaScript(可以阅读它,但不总是写)我的问题是三重。
用户需要输入(提示)“高单拿铁”。
1.我想给这个问题添加一个数组来存储。)咖啡串和b)咖啡的价格。
2.我想用for循环输出迄今为止订购的咖啡总量。
3.我的输出应该是表格格式,例如。

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

Javascript:通过阵列循环

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

您可以完全限定数组,当你做它,就像'VAR大小= ['large','medium','small']',它可能对你的“咖啡”使用一个对象而不是数组是有意义的:'coffee:{sizes:[...],prices: [...],...};然后访问'coffee.sizes.indexOf(userinput [0])' – 2014-11-22 00:21:29

+1

@Lashane这样的东西没有任何意义。 OP使用'var'是正确的。虽然把'var's放入'if's是主观的。 – soktinpk 2014-11-22 00:52:50

+0

@soktinpk'var shots' x 3,'var size' x 3;它不会抛出错误,但它不是_correct_。 – Mathletics 2014-11-22 00:54:18

回答

0

我根据这个答案是什么似乎你想达到(你的第二个代码块)。一切不是-1将强制为true:

-1被称为“标记值”和在javascript一个他们使用~捕获(通常由函数/方法,其有效输出包括0返回)。

进一步解释为注释代码:

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

现在..真正的挑战是想出办法的解析userinput串,确定什么是有效的&完整的,什么不可以(谢天谢地,你没有要求解决这个问题)。我检查了是否设置了sizeshots

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

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