Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-16-2012, 03:53 PM   PM User | #1
niralupatel
New to the CF scene

 
Join Date: Feb 2012
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
niralupatel is an unknown quantity at this point
Unhappy Help in converting javascript

I am trying to convert following pesudocose to javascript. Dont know where am i messing it up. Any help will be great

PSEUDOCODE:

Program TaxCalc
Read purchase

If(purchase>=0 AND purchase<=7)
tax=0
else if(purchase>=8 AND purchase<=21)
tax=1
else if(purchase>=22 AND purchase<=35)
tax=2
else if(purchase>=36 AND purchase<=49)
tax=3
else if(purchase>=50 AND purchase<=64)
tax=4
else if(purchase>=65 AND purchase<=78)
tax=5
else if(purchase>=79 AND purchase<=92)
tax=6
else if(purchase>=93 AND purchase<=99)
tax=7
else print error the number should be >=0 and <=99

print tax

End Program

JAVASCRIPT that I have so far


<html>
<body>
<script type="text/javascript">
// Program name: Tax Calculation
// Purpose: Find tax for amounts under $1
// Name: Niral Patel
// Date: 2/15
START
//Declare variables
var purchase; //Amount to be entered by user
var tax; //Tax to be calculated

//Welcome the user
//Ask them to enter an amount less than a dollar

document.write("This program will help you calculate tax" + BR);
document.write("Please enter the amount, the amount has to be less than a dollar" + BR);

purchase = prompt("Enter the amount for which tax is to be calculated:",ES);
purchase = parseInt(purchase)


if (purchase >= 0 ) {
tax = 0
}

else if (purchase >= 7 ) {
tax = 0
}

else if (purchase >= 21 ) {
tax = 2
}

else if (purchase >= 35 ) {
tax = 3
}

else if (purchase >= 49) {
tax = 4
}

else if (purchase >= 64 ) {
tax = 5
}

else if (purchase >= 78 ) {
tax = 6
}

else if (purchase >= 92 ) {
tax = 7
}

else if (purchase >= 93 ) {
tax = 6
}

else if (purchase >= 99 ) {
tax = 6
}



else {
document.write("Number should be between 0 and 99." +BR);
}
display tax;

Stop


</script>
</body>
</html>
niralupatel is offline   Reply With Quote
Old 02-16-2012, 05:58 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Homework! Many errors, I am afraid.

document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

The Javascipt for "and" is &&.
if (purchase>=0 && purchase<=7) {tax = 0}

There is no Javascript method "print" or "display"

I don't understand the message ("Please enter the amount, the amount has to be less than a dollar"). Nor why if purchase >= 92 tax is 7 but if purchase >= 93 tax is 6.

BR must be in quotes "<br>"
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 02-17-2012, 11:09 AM   PM User | #3
Cremator
New Coder

 
Join Date: Jan 2012
Location: in the uk.
Posts: 99
Thanks: 2
Thanked 6 Times in 4 Posts
Cremator has a little shameless behaviour in the past
> is greater than
>= greater than or equal to

what you should be doing is <= or <

Code:
function taxVal(x){
var taxR = false;
if( x<0 || x>99) return "Number Should be between 0 and 99";
if( x<=7 ) taxR = 0;
if( x<=21 ) taxR = 2;
if( x<=35 ) taxR = 3;
... ans so on
return taxR;
}
alert( taxVal(100) );
then pass the function a number, if its in range the tax value is returned otherwise the message, all you do is test if the return value is numeric or not and then you can output the message or use the value returned
Cremator is offline   Reply With Quote
Old 02-17-2012, 11:46 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Cremator View Post
> is greater than
>= greater than or equal to

what you should be doing is <= or <

Code:
function taxVal(x){
var taxR = false;
if( x<0 || x>99) return "Number Should be between 0 and 99";
if( x<=7 ) taxR = 0;
if( x<=21 ) taxR = 2;
if( x<=35 ) taxR = 3;
//... ans so on
return taxR;
}
alert( taxVal(100) );
then pass the function a number, if its in range the tax value is returned otherwise the message, all you do is test if the return value is numeric or not and then you can output the message or use the value returned
No, that is not correct. The tax is always the highest possible value!


Code:
<script type = "text/javascript">

function taxVal(x){
x = Number(x) || 0;
if( x<0 || x>99) return "Number Should be between 0 and 99";
var taxR = 0;
if( x>=8 ) taxR = 1;
if( x>=22 ) taxR = 2;
if( x>=36 ) taxR = 3;
//... and so on
return taxR;
}

alert( taxVal(6) );  // 0
alert( taxVal(37) ); // 3
</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 02-17-2012, 11:52 AM   PM User | #5
Cremator
New Coder

 
Join Date: Jan 2012
Location: in the uk.
Posts: 99
Thanks: 2
Thanked 6 Times in 4 Posts
Cremator has a little shameless behaviour in the past
This does though
Code:
function taxVal(x){
var taxR = false;
if( x<0 || x>99) return "Number Should be between 0 and 99";
if(!taxR && x<=7 ) taxR = 0;
if(!taxR && x<=21 ) taxR = 2;
if(!taxR && x<=35 ) taxR = 3;
//... ans so on
return taxR;
}
alert( taxVal(100) ); 
alert( taxVal(6) );
The main problems, getting ahead of myself and I left my glasses at work and I am not back at work for a few weeks, so your going to have to put up with typos'
Cremator is offline   Reply With Quote
Old 02-17-2012, 11:55 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Cremator View Post
The main problems, getting ahead of myself and I left my glasses at work and I am not back at work for a few weeks, so your going to have to put up with typos'
And poor grammar as well, I suppose.

The code is still wrong!

Ah! Typos! Not just completely incorrect and untested code, then.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-17-2012 at 12:35 PM..
Philip M is offline   Reply With Quote
Old 02-17-2012, 02:29 PM   PM User | #7
Cremator
New Coder

 
Join Date: Jan 2012
Location: in the uk.
Posts: 99
Thanks: 2
Thanked 6 Times in 4 Posts
Cremator has a little shameless behaviour in the past
Like I said, the OP can sort out whats wrong which is very simple to do as well.
Cremator is offline   Reply With Quote
Old 02-20-2012, 01:11 AM   PM User | #8
niralupatel
New to the CF scene

 
Join Date: Feb 2012
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
niralupatel is an unknown quantity at this point
thanks people! I finally was able to run it properly
niralupatel is offline   Reply With Quote
Old 02-20-2012, 08:03 AM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Cremator View Post
Like I said, the OP can sort out whats wrong which is very simple to do as well.
Do you think your doctor, car repair garage, or plumber could stay in business with that attitude? Or indeed your employer? If you cannot be bothered to test your code before posting, then it would be better to keep quiet. I would be ashamed to offer stuff like that.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:52 PM.


Advertisement
Log in to turn off these ads.