0

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

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Dwillich87
0

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) );
    }
0

Hey ddymacek

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.