Hey

Dean here again, I've been assigned to make an earnings calculator with user input I'm just having trouble in making the final sum work I just get NaN for an answer

function getValue() {

	var values = new Array(239.99, 129.75, 99.95, 350.89);
	var name = prompt("Enter your name: ","name here");
	var quan1 = prompt("How many of item 1 were sold : ","Quanity sold");
	var quan2 = prompt("How many of item 2 were sold : ","Quanity sold");
	var quan3 = prompt("How many of item 3 were sold : ","Quanity sold");
	var quan4 = prompt("How many of item 4 were sold : ","Quanity sold");
	var com1 = alert("Your commission on item 1 is : " + Math.round(((values[0] * quan1) * 0.09) * 100) / 100);
	var com2 = alert("Your commission on item 2 is : " + Math.round(((values[1] * quan2) * 0.09) * 100) / 100);
	var com3 = alert("Your commission on item 3 is : " + Math.round(((values[2] * quan3) * 0.09) * 100) / 100);
	var com4 = alert("Your commission on item 4 is : " + Math.round(((values[3] * quan4) * 0.09) * 100) / 100);
	alert("Total earnings for the week for: " + name + (parseFloat(com1) + parseFloat(com2) + parseFloat(com3) + parseFloat(com4) + 200) );
}

Any help will be appreciated

Thanks

Dean

Recommended Answers

All 2 Replies

your current com1 -> com4 values are strings, not numbers, therefore NaN.
set your numbers in separate variables, then you can work with them later.

function getValue() {
     
    var values = new Array(239.99, 129.75, 99.95, 350.89);
    var name = prompt("Enter your name: ","name here");
    var quan1 = prompt("How many of item 1 were sold : ","Quanity sold");
    var quan2 = prompt("How many of item 2 were sold : ","Quanity sold");
    var quan3 = prompt("How many of item 3 were sold : ","Quanity sold");
    var quan4 = prompt("How many of item 4 were sold : ","Quanity sold");
	var com1 = Math.round((((values[0] * quan1) * 0.09) * 100) / 100);
	var com2 = Math.round((((values[1] * quan2) * 0.09) * 100) / 100);
	var com3 = Math.round((((values[2] * quan3) * 0.09) * 100) / 100);
	var com4 = Math.round((((values[3] * quan4) * 0.09) * 100) / 100);
    var alert1 = alert("Your commission on item 1 is : " + com1);
    var alert2 = alert("Your commission on item 2 is : " + com2);
    var alert3 = alert("Your commission on item 3 is : " + com3);
    var alert4 = alert("Your commission on item 4 is : " + com4);
    alert("Total earnings for the week for: " + name + " " + (parseFloat(com1) + parseFloat(com2) + parseFloat(com3) + parseFloat(com4) + 200) );
    }

Hey ddymacek

I didn't even see your post til now but that is what I ended up doing thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.