Así que estoy tratando de tomar userResponse del indicador, convertirlo en un número entero, luego tomar esa respuesta y agregarla a mi total. Siempre que mi total sea inferior a 35, debería seguir en bucle. Una vez que el total de UserResponse haya llegado a 35, el userResponse debe insertarse en la matriz ShoppingCart y luego el bucle debe romperse y aparecer una alerta de que han alcanzado el umbral de 35. (por cierto, lo siento, soy nuevo en stackoverflow, disculpe mi novato).
//==== VARIABLES ======== let shoppingCart = []; let shippingThreshhold = 35; let userResponse; let total; //==== LOGIC ======== //CHECK FOR ITEMS UNTIL THRESHOLD IS MET. while (total < shippingThreshhold){ //GET ITEM COST FROM USER let userResponse = prompt("What is the value of the item you have selected?"); //CONVERT USER INPUT TO A NUMBER userResponse.parseInt(); //ADD ITEM COST TO RUNNING TOTAL VARIABLE let total = shippingThreshhold - userResponse //PUSH ITEM COST TO CART ARRAY userResponse.concat(shoppingCart); } //SEND POPUP MESSAGE TO USER alert("Your shipping for this order will be free!");Hay algunos problemas con su código:
totaltotal correctamenteuserResponse usando parseInt correctamenteuserResponse al carrito de shoppingCart correctamenteuserResponse globalmente.Algunas mejoras que también podrías hacer:
total , ya que puede usar el carrito de shoppingCart para calcularlo (pero mantuve el total para que sea más simple).shippingThreshold usando un formato de nomenclatura convencional para constantes (todas en mayúsculas separadas por guiones bajos).const en lugar de let cuando sea posible, para evitar sobrescribir involuntariamente los valores de las variables. //==== VARIABLES ======== const shoppingCart = []; const SHIPPING_THRESHOLD = 35; let total = 0; //==== LOGIC ======== //CHECK FOR ITEMS UNTIL THRESHOLD IS MET. while (total < SHIPPING_THRESHOLD) { //GET ITEM COST FROM USER const userResponse = prompt(`Your cart: ${shoppingCart.length ? shoppingCart : 'empty' }. What is the value of the item you have selected?`); if (userResponse === null) { break } else { //CONVERT USER INPUT TO A NUMBER const itemCost = parseInt(userResponse); if (Number.isNaN(itemCost)) { alert(`"${userResponse}" is not a number please try again or press cancel to exit.`) } else { // ADD TO TOTAL total += itemCost //PUSH ITEM COST TO CART ARRAY shoppingCart.push(itemCost); } } } //SEND POPUP MESSAGE TO USER if (total >= SHIPPING_THRESHOLD) { alert(`Your shipping for this order will be free! Your cart: ${shoppingCart}`); } else { alert("Thanks for visiting!") }