/    Sign up×
Community /Pin to ProfileBookmark

Making a prompt calculations need help Plz

Was making a prompt using 5 product numbers and when i dont use one of the five my finial calculation for that one i didnt use turns into undefined value what do i do to change it to Number 0.

<?xml version = “1.0” encoding = “utf-8”?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>

<html>
<head>
<style <“text/css”>

th { text-align:left;}
table { width:100% }

</style>

<script type = “text/javascript”>
<!–
var quantity;
var choice;
var total1;
var total2;
var total3;
var total4;
var total5;
var productnumber;

while ( choice != 0)
{
choice = window.prompt( “Enter product number [1-5] [0 to stop]:” , “” );

switch ( choice )
{
case “1”:
quantity = parseInt(window.prompt( “Enter quantity sold:”, “” ));
productnumber = 2.98;
total1 = quantity * productnumber;
break;

case “2”:
quantity = parseInt(window.prompt( “Enter quantity sold:”, “” ));
productnumber = 4.50;
total2 = quantity * productnumber;
break;

case “3”:
quantity = parseInt(window.prompt( “Enter quantity sold:”, “” ));
productnumber = 9.98;
total3 = quantity * productnumber;
break;

case “4”:
quantity = parseInt(window.prompt( “Enter quantity sold:”, “” ));
productnumber = 4.49;
total4 = quantity * productnumber;
break;

case “5”:
quantity = parseInt(window.prompt( “Enter quantity sold:”, “” ));
productnumber = 6.87;
total5 = quantity * productnumber;
break;
}

if ( choice > 5 )
window.alert(“Enter product number 1-5 or 0 to stop”, “” );

}

document.writeln( “<h2>Product Totals</h2>” );

document.writeln( “<table border = “1”>”);

document.writeln( “<thead><tr><th>Product#</th>” );

document.writeln( “<th>Total Sales</th>” );

document.writeln( “</tr><thead><tbody> ” );

document.writeln( “<tr><td>Product1</td><td>” + total1 + “</td></tr>” );
document.writeln( “<tr><td>Product2</td><td>” + total2 + “</td></tr>” );
document.writeln( “<tr><td>Product3</td><td>” + total3 + “</td></tr>” );
document.writeln( “<tr><td>Product4</td><td>” + total4 + “</td></tr>” );
document.writeln( “<tr><td>Product5</td><td>” + total5 + “</td></tr>” );
document.writeln( “</tbody></table>” );
// –>

</script>

</head>
<body>
</body>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KasumiSaraiNov 17.2009 — Edit: Nevermind, this will only work if no values are entered for any of the choices. Hm. Let me think.
Copy linkTweet thisAlerts:
@KasumiSaraiNov 18.2009 — Bah. I recall you can set an undefined value for a variable to 0 with php, but not finding a way to do it with javascript, except for setting it as a function, which seems to throw off the rest of the script you are using. At least to me. Sorry about that, seems I am of no help afterall. At least you get a bump out of me. Lol.
×

Success!

Help @Hoppy2008 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...