/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Code not working with parseInt & don’t know how to restart code after failing a check

Assignment: A3 Task3: Write your Own Code
Write a JavaScript program (T3.html) to compute the commission based on the sales value.
The commission is calculated as:
Sales value >=3000 -> 4% of sales value
Sales value >= 2000 -> 3% of sales value
Sales value >=1000 -> 2% of sales value
Sales value <1000 and >0 -> fixed value : $8.00
Sales value <=0 -> $0.00
The program must have the following features:
• Prompt the user to enter a sales value;
• Save the value entered into a variable.
• After the user input is recorded into a variable, the program should check few things as follow:
The program should check if the value entered is a number.
The program should check if the value entered is between 0-6000.
Finally, if value entered is outside the limits or not a number, display a message saying “Sales
Value must be between 0-6000 only. Please re-enter the value”. Note: (use alert to display the
message).
• If the value entered it is a number between 0-6000 , compute the commission and display the value in an alert
message
o Here is an output example to help you display the message:
The sales value is $5000. The commission is 4% of sales value.
The commission value is $200.
• Use Comments explaining how the program works

[SIZE=4][/SIZE][B][COLOR=”#0000CD”]My Code:[/COLOR][/B]

[CODE]<script type=”text/javascript”>

var mySales = prompt(“Enter your total sales”, 0);
//var = parseInt(mySales); //If I try to use the code with this line in, the code will not run
var percent = 0.0;
var fixed = 0.00;

if (isNaN(mySales))
{
alert(“Sales Value must be betwen 0-6000 only. Please re-enter the value.”);
}

else if (!(mySales >=0 && mySales <=6000))
{
alert(“Sales Value must be betwen 0-6000 only. Please re-enter the value.”);
}

//As per the assignment I need to check that the value entered is a number and that the value entered is between 0 and 6000.
//How do I stop the rest of the code from running after the alert and just reset to the beginning prompt of entering a value?

if (mySales <= 0)
{
percent = .00;
}

else if (mySales >0 && mySales < 1000)
{
fixed = 8.00;
}

else if (mySales >= 1000 && mySales < 2000)
{
percent = .02;
}

else if (mySales >= 2000 && mySales < 3000)
{
percent = .03;
}

else if (mySales >= 3000)
{
var percent = .04;
}

var commission = mySales * percent || fixed;

alert(” The sales value is $” + parseInt (mySales) + “.” + “n” + ” The commission is ” + (percent * 100) + “% of sales value.” + “n” + ” The commission value is $” + commission + “.00.”);

</script>[/CODE]

Thank you!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Kevin2Oct 12.2014 — &lt;script type="text/javascript"&gt;

<i> </i> var mySales = prompt("Enter your total sales", 0);
<i> </i> var percent = 0.0;
<i> </i> var fixed = 0.00;



<i> </i> if ((isNaN(mySales)) || ((mySales &lt;=0) || (mySales &gt;=6000)))
<i> </i> {
<i> </i> alert("Sales Value must be between 0-6000 only. Please re-enter the value.");
<i> </i> window.location.reload();
<i> </i> }

<i> </i> else {

<i> </i> if (mySales &lt;= 0)
<i> </i> {
<i> </i> percent = .00;
<i> </i> }

<i> </i> else if (mySales &gt;0 &amp;&amp; mySales &lt; 1000)
<i> </i> {
<i> </i> fixed = 8.00;
<i> </i> }

<i> </i> else if (mySales &gt;= 1000 &amp;&amp; mySales &lt; 2000)
<i> </i> {
<i> </i> percent = .02;
<i> </i> }

<i> </i> else if (mySales &gt;= 2000 &amp;&amp; mySales &lt; 3000)
<i> </i> {
<i> </i> percent = .03;
<i> </i> }

<i> </i> else if (mySales &gt;= 3000)
<i> </i> {
<i> </i> var percent = .04;
<i> </i> }

<i> </i> var commission = mySales * percent,
<i> </i> commission2 = commission.toFixed(2);


<i> </i> alert(" The sales value is $" + parseInt (mySales) + "." + "n" + " The commission is " + (percent * 100) + "% of sales value." + "n" + " The commission value is $" + commission2);

<i> </i> }
<i> </i>&lt;/script&gt;
Copy linkTweet thisAlerts:
@BlondieCauthorOct 12.2014 — Hi Kevin and thanks for your help! When I tried this the only variable that doesn't run correctly is the fixed $8.00 amount if the amount entered is >0 or <1000. Thanks also for showing me how to set the decimals on the commission.
Copy linkTweet thisAlerts:
@BlondieCauthorOct 12.2014 — Hi Kevin... found it and have it running now. Thank you for taking the time to help me with this. It's been more of a challenge than I expected.
Copy linkTweet thisAlerts:
@deathshadowOct 13.2014 — Some other advice, don't waste time checking values you already know are true! You have an else, so you already would know (for example) that it's >= 1000 since you JUST did a < 1000.

Also not sure your logic is correct for the logical "OR" on your result.

var
mySales,
percent = 0,
fixed = 0,
errorText = '';

<i> </i>do {
<i> </i> mySales = prompt(errorText + 'Enter your total sales', 0);
<i> </i> if (isNaN(mySales) || (mySales &lt; 0) || mySales &gt; 6000)) {
<i> </i> errorText = "Sales Value must be betwen 0-6000 only. Please re-enter the value.rn";
<i> </i> }
<i> </i>} while (errorText != '');

<i> </i>if ((mySales == 0) {
<i> </i> percent = 0;
<i> </i>} else if (mySales &lt; 1000) {
<i> </i> fixed = 8.0;
<i> </i>} else if (mySales &lt; 2000) {
<i> </i> percent = 0.2;
<i> </i>} else if (mySales &lt; 3000) {
<i> </i> percent = 0.3;
<i> </i>} else percent = 0.4;

<i> </i>var commission = fixed || mySales * percent;


WAY simpler... generates the same values.

1) We don't let them out of the prompt until they enter 0...6000

2) we no longer need to test less than zero... so just test for zero...

3) we know it's greater than zero, so just test for < 1000

4) we KNOW it's not < 1000, so it MUST be above/equal to 1000, so test for < 2000.

lather, rinse, repeat. The last one we don't even need a test condition on, since we trapped it from allowing values over 6000 and we already KNOW it's going to be >= 3000... hence "else".

When you already tested a value with the previous condition in nested IF, you don't need to check the opposite on the next statement as you already know it's true!

Also, TRY to avoid forcing a reload, complete and utter waste of bandwidth.
Copy linkTweet thisAlerts:
@BlondieCauthorOct 13.2014 — Thank you Deathshadow! Completely different from how I did my code. The difference between the experienced and the student learning from an Instructor who doesn't provide much help and direction. Now tackling the next multi-task assignment for the course.
×

Success!

Help @BlondieC 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 5.22,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...