JavaScript help :P

JavaScript program not running

Page 1 of 1

6 Replies - 1283 Views - Last Post: 01 May 2010 - 03:20 AM

#1 IrishCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 07-April 10

JavaScript help :P

Posted 30 April 2010 - 06:37 AM

Hi all, could you just point out to me the possible reason why this code is not running?
I have had a good hard look and cant see any faults, any help would be much appreciated

<html>
<head>
<title>Loops</title>

<script type = "text/javascript">

var name, age, income, cost

name = window.prompt("Enter your name")
age = window.prompt("Enter your age")

do
{
  parseInt(income) = window.prompt("Enter monthly income")
 
  if(income >= 0 && income <= 3000)
  {
    if(age >= 60)
    {
     cost = 50
    }
    else if (age <= 59 && age >= 40)
    {
     cost = income * 0.085
    }
    else
    {
     cost = (income * 0.1) + 95
    }
  else
   { 
    cost = 0
   }
  }

}while(income > 0)

document.write("</br>PATIENT NAME : " + name)
document.write("</br>PATIENT AGE  : " + age)
document.write("</br>MONTHLY INCOME : R" + income)
document.write("</br>COST OF TREATMENT : R" + cost)

</script>
</head>
</html>
  


Is This A Good Question/Topic? 0
  • +

Replies To: JavaScript help :P

#2 IrishCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 07-April 10

Re: JavaScript help :P

Posted 30 April 2010 - 06:41 AM

Hi all, just need some insight as to why the code is not running, i have looked through it a few times and cant see any faults
any help would be much appreciated, thanks :)

<html>
<head>
<title>Loops</title>

<script type = "text/javascript">

var name, age, income, cost

name = window.prompt("Enter your name")
age = window.prompt("Enter your age")

do
{
  parseInt(income) = window.prompt("Enter monthly income")
 
  if(income >= 0 && income <= 3000)
  {
    if(age >= 60)
    {
     cost = 50
    }
    else if (age <= 59 && age >= 40)
    {
     cost = income * 0.085
    }
    else
    {
     cost = (income * 0.1) + 95
    }
  else
   { 
    cost = 0
   }
  }

}while(income > 0)

document.write("</br>PATIENT NAME : " + name)
document.write("</br>PATIENT AGE  : " + age)
document.write("</br>MONTHLY INCOME : R" + income)
document.write("</br>COST OF TREATMENT : R" + cost)

</script>
</head>
</html>
  

Was This Post Helpful? 0
  • +
  • -

#3 singularity  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 184
  • Joined: 17-October 08

Re: JavaScript help :P

Posted 30 April 2010 - 06:45 AM

View PostIrishCoder, on 30 April 2010 - 12:37 PM, said:

Hi all, could you just point out to me the possible reason why this code is not running?
I have had a good hard look and cant see any faults, any help would be much appreciated

<html>
<head>
<title>Loops</title>

<script type = "text/javascript">

var name, age, income, cost

name = window.prompt("Enter your name")
age = window.prompt("Enter your age")

do
{
  parseInt(income) = window.prompt("Enter monthly income")
 
  if(income >= 0 && income <= 3000)
  {
    if(age >= 60)
    {
     cost = 50
    }
    else if (age <= 59 && age >= 40)
    {
     cost = income * 0.085
    }
    else
    {
     cost = (income * 0.1) + 95
    }
  else
   { 
    cost = 0
   }
  }

}while(income > 0)

document.write("</br>PATIENT NAME : " + name)
document.write("</br>PATIENT AGE  : " + age)
document.write("</br>MONTHLY INCOME : R" + income)
document.write("</br>COST OF TREATMENT : R" + cost)

</script>
</head>
</html>
  



Do you know that you are in a Java forum. You should have posted it in Javascript Forum.
As Dogstopper says Java != Javascript :)
Was This Post Helpful? 0
  • +
  • -

#4 revolutionx  Icon User is offline

  • D.I.C Head

Reputation: 60
  • View blog
  • Posts: 229
  • Joined: 23-July 09

Re: JavaScript help :P

Posted 30 April 2010 - 07:04 AM

Does it just need to ask once for all the information and then print it out ?
Was This Post Helpful? 0
  • +
  • -

#5 CamoDeveloper  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 20
  • View blog
  • Posts: 250
  • Joined: 12-June 09

Re: JavaScript help :P

Posted 30 April 2010 - 07:18 AM

You need to put semicolons at the end of your lines like so:

<html>
	<head>
		<title>Loops</title>
		<script type = "text/javascript">
			var name, age, income, cost
		
			name = window.prompt("Enter your name");
			age = window.prompt("Enter your age");

			do{
				parseInt(income) = window.prompt("Enter monthly income");
				if(income >= 0 && income <= 3000){
					if(age >= 60){
						cost = 50;
    					}else if(age <= 59 && age >= 40){
     						cost = income * 0.085;
	    				}else{
     						cost = (income * 0.1) + 95;
    					}else{ 
						cost = 0;
   					}
  				}
			}while(income > 0);

			document.write("<br />PATIENT NAME : " + name);
			document.write("<br />PATIENT AGE  : " + age);
			document.write("<br />MONTHLY INCOME : R" + income);
			document.write("<br />COST OF TREATMENT : R" + cost);
		</script>
	</head>
</html>


This post has been edited by CamoDeveloper: 30 April 2010 - 07:19 AM

Was This Post Helpful? 0
  • +
  • -

#6 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: JavaScript help :P

Posted 30 April 2010 - 02:48 PM

CamoDeveloper, you don't need semicolons in Javascript. They're not required, although it does help.

The problem with this code is that you can't have more than one else in an if-else block..

[edit]
Oh but wait, the last else was supposed to go to the parent block. You're missing a curly brace:
if (income >= 0 && income <= 3000){
	if (age >= 60) {
		cost = 50;
	}
	else if (age <= 59 && age >= 40) {
		cost = income * 0.085;
	}
	else {
		cost = (income * 0.1) + 95;
	}
}
else { 
	cost = 0;
}

This post has been edited by forest51690: 30 April 2010 - 02:53 PM

Was This Post Helpful? 1
  • +
  • -

#7 IrishCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 07-April 10

Re: JavaScript help :P

Posted 01 May 2010 - 03:20 AM

Thanks for all the replies :) the code is working perfectly now.
Yeah forest i forgot to close the first If statement first so it was missing a curly bracket, thanks alot
This is the final working code

<html>
<head><title>Program</title>
<script type = "text/javascript">

var name, income, age, cost

name = window.prompt("Enter name...")
age = window.prompt("Enter age...")

do
{
 income = window.prompt("Enter Income...")

if(income <= 3000 && income > 800)
{  
 if(age >= 60)
 {
  cost = 50
 }
 else if (age >= 40)
 {
  cost = 0.085 * income
 }
 else
 {
  cost = (0.1 * income) + 95
 }
 
}

else
{ 
 cost = 0 
}
 
}while(income < 0)
 
 document.write("</br>PATIENT NAME : " + name)
 document.write("</br>PATIENT AGE : " + age)
 document.write("</br>MONTHLY INCOME : R" + income)
 document.write("</br>TREATMENT COST : R" + Math.round(cost, 2))

</script>
</head>
</html>


Works like a dream :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1