Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
free shipping ... if shopping chart total is greater than $35 ... user input unless total is more $35 in JavaScript

I m not sure what i m doing wrong. Thanks in advance for helping me in this matter.

var sum = 0;
var pricecheck = 35;
while (sum < pricecheck) {
  var userinput = prompt("Please enter the cost of the item...");
  var num1 = parseInt(userinput);
  submitprice.push(num1);
  for (i = 0; i < submitprice.length; i++) {
    sum += submitprice[i];
  }
}
alert("free shipping.");

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Declare sum to zero each time you are executing the sum of items. Or else it will keep on adding to the sum that was calculated in previous iteration.

Also your submitprice seems to be undefined. I have initilaized it as an empty array.

Working Fiddle

var sum = 0;
var pricecheck = 35;
const submitprice = [];
while (sum < pricecheck) {
    var userinput = prompt("Please enter the cost of the item...");
    var num1 = parseInt(userinput);
    submitprice.push(num1);
    sum = 0;
    for (i = 0; i < submitprice.length; i++) {
        sum += submitprice[i];
    }
}
alert("free shipping.");

about 4 years ago · Juan Pablo Isaza Relatório

0

To start, you need to create an empty array to store the totals in. For this, I will call it "cart":

var cart = [];

Next, I would suggest creating an if statement to check if the input is a number:

var num1 = parseInt(userinput);
if(isNaN(userinput){
    alert("Please enter a number");
    continue;
}

You don't need the for loop to add the input to the sum, just remove the loop:

sum += userinput

After the loop, you would push to the cart:

cart.push(num1)

Finally, you need to check if the sum is more than free shipping

if(sum >= pricecheck) {
    alert("You qualify for free shipping!")'
}

Then just output the result to the console with a pipe (|) concatenated between the numbers.

console.log(cart.join(" | ")

var sum = 0;
var cart = [];
var pricecheck = 35;

while (sum < pricecheck) {
  var userinput = prompt("Please enter the cost of the item...");
  if (userinput === null) {
    break;
  }
  var num1 = parseInt(userinput);
  if (isNaN(userinput)) {
    alert("Please enter a number");
    continue;
  }
  cart.push(num1);
  sum += num1;
}
if (sum >= pricecheck) {
  alert("free shipping.");
}

about 4 years ago · Juan Pablo Isaza Relatório

0

I took a look at this assignment again to refresh on what was asked for this question. You do not need to have that for loop at all within the while loop. Within each iteration of the while loop, you are asking the user for the price of their next item, adding it to the shopping cart, and also adding it to a running total (this total being your "sum"). This running total does not need to be re-calculated each time inside of the while loop because you would have done that in a single line. You are trying to calculate the price, starting at 0, each time you loop through the while loop (using your for loop). But this is not needed as you already have a running total that can be added to each time the user enters a value.

"Austin Caron" explained it well but for this assignment we do not need to do user authentication.

P.S. Try your best to avoid asking questions directly about an assignment and ask more general questions about a concept or idea you are struggling with.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda