Home > OS >  Javascript's while loop stays in an infinite loop and doesn't exit any of the conditionals
Javascript's while loop stays in an infinite loop and doesn't exit any of the conditionals

Time:08-01

In this code I can't find the reason why the while loop doesn't end. Perhaps it is because I have incorrectly formulated the conditionals within it, but the idea was that in each conditional the cycle would be repeated and it would ask again which of the models you want to select and if not, finish with "end".

class JordansRetro {
    constructor (model, price){
        this.model = model;
        this.price = price;
        
    }
    addTax(){
        this.price = this.price * 1.5;
    }
}

const stock = []
stock.push(new JordansRetro("Jordan 1", 140))
stock.push(new JordansRetro("Jordan 2", 240))
stock.push(new JordansRetro("Jordan 3", 440))
 
for(const JordansRetro of stock ){
    JordansRetro.addTax
}

let buy = prompt ("We have 3 models of shoes for sale: which one do you want to buy?")

function toShow(message){
    console.log(message)
    alert(message)
}
  
while (buy != "end"){
    if (buy == "Jordan 1"){
        stock.find(JordansRetro => {
            if (JordansRetro.model === "Jordan 1"){
                toShow(JordansRetro.model   " "   JordansRetro.price)}
            
        })
    }
    else if (buy == "Jordan 2"){
        stock.find(JordansRetro => {
            if (JordansRetro.model === "Jordan 2"){
                toShow(JordansRetro.model   " "   JordansRetro.price)}
            
        })
    }
    else if (buy == "Jordan 3"){
        stock.find(JordansRetro => {
            if (JordansRetro.model === "Jordan 3"){
                toShow(JordansRetro.model   " "   JordansRetro.price)}
            
        })
    }
    else{
        toShow("We don't have that model")
    }
}

CodePudding user response:

buy is only set once outside of the loop. You need to prompt for it again at the end of the loop like this:

class JordansRetro {
    constructor (model, price){
        this.model = model;
        this.price = price;
        
    }
    addTax(){
        this.price = this.price * 1.5;
    }
}

const stock = []
stock.push(new JordansRetro("Jordan 1", 140))
stock.push(new JordansRetro("Jordan 2", 240))
stock.push(new JordansRetro("Jordan 3", 440))
    
for(const JordansRetro of stock ){
    JordansRetro.addTax
}

let buy = prompt ("We have 3 models of shoes for sale: which one do you want to buy?")

function toShow(message){
    console.log(message)
    alert(message)
}
    
while (buy != "end"){
    if (buy == "Jordan 1"){
        stock.find(JordansRetro => {
            if (JordansRetro.model === "Jordan 1"){
                toShow(JordansRetro.model   " "   JordansRetro.price)}
            
        })
    }
    else if (buy == "Jordan 2"){
        stock.find(JordansRetro => {
            if (JordansRetro.model === "Jordan 2"){
                toShow(JordansRetro.model   " "   JordansRetro.price)}
            
        })
    }
    else if (buy == "Jordan 3"){
        stock.find(JordansRetro => {
            if (JordansRetro.model === "Jordan 3"){
                toShow(JordansRetro.model   " "   JordansRetro.price)}
            
        })
    }
    else{
        toShow("We don't have that model")
    }

    buy = prompt ("We have 3 models of shoes for sale: which one do you want to buy?")
}
  • Related