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;
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.
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
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.
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'
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.
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.