简体   繁体   中英

Javascript: Looping through an array

This is driving me crazy! I'm very new to javascript(can read it but not always write it) My problem is threefold.
User needs to input(prompt) "tall single latte".
1. I want to ad an array to this problem to store a.) the coffee strings and b.) the coffee prices.
2. I want to use a for loop to output the total amount of coffee ordered so far.
3. My output should be in table format ex.

short single latte's price is R10
double tall coffee's price is 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);  
}

An example of what I want to achieve:

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

I based this answer on what appears that you want to achieve (your second code-block).

-1 (often returned by functions/methods whose valid output includes 0) is called a 'sentinel value' and in javascript one catches them using ~ : everything that is not -1 will coerce to true.

Further explanations as comments in code:

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

Now.. the real challenge is thinking up way's to parse the userinput strings, determining what is valid & complete and what not (thankfully you didn't ask for solutions to that problem). I checked if size and shots are set.

Hopefully this helps your learning experience.

Thank you guys, I think I figured it out.. know its probably the long way... but...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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