Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
nest if./else if statement default to last else even if parameters above are true
Pizza.prototype.getPrice = function() {
  const price = 10
  if (this.pizzaSize === "medium") {
    if (this.toppings === "cheese") {
      console.log(price + 6);
    } else if (this.toppings === ("cheese", "pepperoni")) {
      console.log(price + 7);
    } else {
      console.log(price + 8);
    }
  } else if (this.pizzaSize === "large")  {
    if (this.toppings === "cheese") {
      console.log(price + 11);
    } else if (this.toppings === ("cheese", "pepperoni")) {
      console.log(price + 12);
    } else {
      console.log(price + 13);
    }
  } else if (this.pizzaSize === "xlarge") {
    if (this.toppings === "cheese") {
      console.log(price + 15);
    } else if (this.toppings === ("cheese", "pepperoni")) {
      console.log(price + 16);
    } else {
      console.log(price + 17);
    }
  }
}

function Pizza(pizzaSize, toppings) {
  this.pizzaSize = pizzaSize;
  this.toppings = toppings;
}

//UI Logic ---
function displayPizzaInfo(pizzaToDisplay) {
  let pizzaInfo = $("p#output");
  let htmlForPizzaInfo = pizzaToDisplay.pizzaSize + " " + pizzaToDisplay.toppings + " Pizza" + " " + "$ " + pizzaToDisplay.getPrice();
  pizzaInfo.html(htmlForPizzaInfo);
}

$(document).ready(function(){
  $("form#pizzaOrder").submit(function(event) {
    event.preventDefault();
    const pizzaSize = $("#pizzaSize").val();
    let ele = [];
    $("input:checkbox[name=topping]:checked").each(function() {
      ele.push($(this).val());
    })
    let myPizza = new Pizza(pizzaSize, ele)
    displayPizzaInfo(myPizza);
    // let myPizzaPrice = myPizza.getPrice();
    });
}); 

let myPizza = new Pizza("medium", "cheese");
myPizza.getPrice();
Expected Output: 
16

The test above no longer applies and the expected output becomes 18. I do believe either the way I'm writing my nested ifs or the if's are structured incorrectly. I did TDD and as I wrote my tests out they all worked not realizing my previous tests were no longer working. I'm looking to get my prototype.getPrice to match my if statements and therefore correspond correctly with what the user sees on the web application. Any help would be greatly appreciate. Thank you!

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda