So im trying to take userResponse from prompt, convert it into an integer, then take that response and add it to my total. As long as my total is under 35 it should keep looping. Once the total userResponse has reached 35, the userResponse should be pushed into shoppingCart array and then the loop should break and an alert appear that they have reached the threshhold of 35. (btw sorry I'm new to stackoverflow, excuse my newbieness).
//==== 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!");
There are a few issues with your code:
totaltotal properlyuserResponse using parseInt properlyuserResponse to the shoppingCart properlyuserResponse globally.Some improvements you could also make:
total, as you can use the shoppingCart to calculate that (but I've kept total to keep it simpler).shippingThreshold constant using a conventional naming format for constants (all caps separated by underscore).const instead of let when possible, to avoid unintentionally overwriting variable values.//==== 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!")
}