javascript numbers assignment

i’m having some issues with the final two console.logs of my script. i’m supposed to have numbers output for both but i’m getting NAN

/* Evin Round
Jan 14, 2023
Let's Go Shopping CE03 */

alert("Let's make a shopping list!");

let first = prompt("What is the first item?");

let firstCost = Number(prompt("What is the cost of " +first+"?"));

let firstAmount = Number(prompt("How many of "+first+" would you like?"));

let second = prompt("What is the second item?");

let secondCost = Number(prompt("What is the cost of " +second+"?"));

let secondAmount = Number(prompt("How many of "+second+" would you like?"));

let tax = parseInt(prompt("What is the sales tax for your state?"));

let firstTotal = parseFloat(firstCost * firstAmount);
let secondTotal = parseFloat(firstCost * firstAmount);
let subTotal = parseFloat(firstTotal + secondTotal);
let taxTotal = parseFloat(subTotal * tax);
let grandTotal = parseFloat(subTotal + taxTotal);

console.log(first +" "+ firstCost +" "+ firstAmount +" "+ firstTotal);
console.log(second +" "+ secondCost +" "+ secondAmount +" "+ secondTotal);
console.log("tax: "+taxTotal);
console.log("TOTAL: "+grandTotal);


i changed all of the Number() to parseFloat() but I’m not getting the outcome I’m looking for.

>Solution :

The problem with your code is in these two lines:

let firstTotal = parseFloat(firstCost * firstAmount);
let secondTotal = parseFloat(firstCost * firstAmount);

You are multiplying the firstCost and firstAmount for both the firstTotal and secondTotal, instead of using the secondCost and secondAmount.

It should be

let firstTotal = parseFloat(firstCost * firstAmount);
let secondTotal = parseFloat(secondCost * secondAmount);

Also it is recommended to use toFixed(2) after the calculation to make sure your numbers only have 2 decimal places.

let firstTotal = parseFloat(firstCost * firstAmount).toFixed(2);
let secondTotal = parseFloat(secondCost * secondAmount).toFixed(2);

You might also want to check what you’re getting from your prompts, check if the input is a number or a string.
Also, you might want to check if the tax value is a number or not.

Also, it is recommended to check for the user’s input, in case the user inputs invalid data, it will prevent the script from breaking and handle the error.

Here is an updated version of your code with some additional error handling and input validation:

alert("Let's make a shopping list!");

let first = prompt("What is the first item?");

let firstCost = parseFloat(prompt("What is the cost of " +first+"?"));
if(isNaN(firstCost)){
    alert("Invalid cost, Please enter a valid number");
    firstCost = parseFloat(prompt("What is the cost of " +first+"?"));
}

let firstAmount = parseFloat(prompt("How many of "+first+" would you like?"));
if(isNaN(firstAmount)){
    alert("Invalid amount, Please enter a valid number");
    firstAmount = parseFloat(prompt("How many of "+first+" would you like?"));
}

let second = prompt("What is the second item?");

let secondCost = parseFloat(prompt("What is the cost of " +second+"?"));
if(isNaN(secondCost)){
    alert("Invalid cost, Please enter a valid number");
    secondCost = parseFloat(prompt("What is the cost of " +second+"?"));
}

let secondAmount = parseFloat(prompt("How many of "+second+" would you like?"));
if(isNaN(secondAmount)){
    alert("Invalid amount, Please enter a valid number");
    secondAmount = parseFloat(prompt("How many of "+second+" would you like?"));
}

let tax = parseFloat(prompt("What is the sales tax for your state?"));
if(isNaN(tax)){
    alert("Invalid tax rate, Please enter a valid number");
    tax = parseFloat(prompt("What is the sales tax for your state?"));
}

let firstTotal = parseFloat(firstCost * firstAmount).toFixed(2);
let secondTotal = parseFloat(secondCost * secondAmount).toFixed(2);
let subTotal = parseFloat(firstTotal + secondTotal).toFixed(2);
let taxTotal = parseFloat(subTotal * (tax/100)).toFixed(2);
let grandTotal = parseFloat(subTotal + taxTotal).toFixed(2);

console.log(first +" "+ firstCost +" "+ firstAmount +" "+ firstTotal);
console.log(second +" "+ secondCost +" "+ secondAmount +" "+ secondTotal);
console.log("tax: "+taxTotal);
console.log("TOTAL: "+grandTotal);

the code checks if the input is a number or not, and prompt the user again if it’s not valid. It also corrects the mistake in the calculation of the secondTotal, and added the toFixed(2) after the calculation to make sure your numbers only have 2 decimal places.

It also calculates the tax based on the subtotal not the total, so it’s more accurate.

Leave a Reply