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 09-09-2012, 05:32 AM   PM User | #1
msNewbie
New Coder

 
Join Date: Sep 2012
Posts: 15
Thanks: 9
Thanked 0 Times in 0 Posts
msNewbie is an unknown quantity at this point
Getting error with debugger

[Code:]
Timestamp: 9/8/2012 11:30:59 PM
Error: SyntaxError: illegal XML character
Source Code:
if(.01 || <1.00)
-----------^
[/Code]

FireFox debugger doesn't like 1.00 or 1.0 or 1
What I am trying to say is:
.
If it is 1˘ or less than $1.00
msNewbie is offline   Reply With Quote
Old 09-09-2012, 06:08 AM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
you are making a comparison but giving the code nothing to compare it to. generally speaking (but not always), the left side will have a variable:

Code:
if(x==.01 || x<1)
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
msNewbie (09-09-2012)
Old 09-09-2012, 06:29 AM   PM User | #3
msNewbie
New Coder

 
Join Date: Sep 2012
Posts: 15
Thanks: 9
Thanked 0 Times in 0 Posts
msNewbie is an unknown quantity at this point
Entire script enclosed

Quote:
Originally Posted by xelawho View Post
you are making a comparison but giving the code nothing to compare it to. generally speaking (but not always), the left side will have a variable:

Code:
if(x==.01 || x<1)
Dang! I forgot to say that I tried that too, sorry bout that!
Here is the entire code.
Everything works all the up to the last section where it declares winner or loser.
Code:
function main()  //program driver
{	// This begins function main()

// This section tells me what the variables are

	var pennies = 0;
	var nickles = 0;
	var dimes   =0;
	var quarters = 0;
	var halfDollars = 0;
	var dollars = 0;
	var t       = 0;

// This section will let the user be prompted for the amount of coin.

	pennies = parseFloat(prompt("Enter how many pennies you have:", ""));
	nickles = parseFloat(prompt("Enter how many nickles you have:", ""));	
	dimes = parseFloat(prompt("Enter how many dimes you have:", ""));	
	quarters = parseFloat(prompt("Enter how many quarters you have:", ""));
	halfDollars = parseFloat(prompt("Enter how many half dollars you have:", ""));	
	dollars = parseFloat(prompt("Enter how many dollars you have:", ""));

	t = .01 * pennies + .05 * nickles + .10 * dimes + .25 * quarters + .5 * halfDollars + 1 * dollars;
		
// Shows total coins
	function outputDeclairAmountTotal(pennies, nickles, dimes, quarters, halfDollars, dollars)
	{
	alert("Pennies: " + pennies + "\n" +
	      "Nickles: " + nickles + "\n" +
		  "Dimes: " + dimes + "\n" +
		  "Quarters: " + quarters + "\n" +
		  "Half Dollars: " + halfDollars + "\n" +
		  "Dollars: " + dollars + "\n" +
	      "Caculated amount: " + t.toFixed(2));
	}
		  
// This will give the results of the game. 
// Equal to 1˘ or less than $1.00 = Sorry! You lose! You have less than one dollar.
// Over one dollar = Sorry! You lose! You have more that one dollar.
// If no coins were put in then = No coins entered.
// If you won then = Congratulations! You win!
	function 
	if(t==.01 || x<1)
		{
			alert("Sorry! You lose! You have less than one dollar.")
		}
		
	else if(t== >1)
		{
			alert("Sorry! You lose! You have more than one dollar.") 
		}
	
	else (1)
	{
	alert("Congratulations! You win! You have exactly one dollar.")
	}
			 
		   
}	//	This ends function main()

Last edited by msNewbie; 09-09-2012 at 06:31 AM..
msNewbie is offline   Reply With Quote
Old 09-09-2012, 07:11 AM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
else if(t== >1)
this syntax is incorrect, and
Code:
else (1)
will always be true.

You should be able to fix this by reading any guide to comparison operators.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
msNewbie (09-09-2012)
Old 09-09-2012, 11:03 AM   PM User | #5
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Code:
function 
	if(t==.01 || x<1)
		{
			alert("Sorry! You lose! You have less than one dollar.")
		}
I would say the error is on the line preceding the test.
Logic Ali is offline   Reply With Quote
Users who have thanked Logic Ali for this post:
msNewbie (09-10-2012)
Old 09-09-2012, 01:25 PM   PM User | #6
msNewbie
New Coder

 
Join Date: Sep 2012
Posts: 15
Thanks: 9
Thanked 0 Times in 0 Posts
msNewbie is an unknown quantity at this point
Angry

Quote:
Originally Posted by AndrewGSW View Post
Code:
else if(t== >1)
this syntax is incorrect, and
Code:
else (1)
will always be true.

You should be able to fix this by reading any guide to comparison operators.
I read the page and I was up until 04:30 doing it in almost every combination that I could figure. Then I wind up waking up out of frustration and trying more (or probably duplicate of this morning) and it still won't work.
I tried with the && . I tried without the && .
I tried spaces. I tried no spaces.
I tried parenthesis. I tried no parenthesis.
I even tried just <.01 and I catch hell from the debugger.
No matter WHAT I tried it just keeps giving me errors and won't even start the program. Grrr! Am I thinking the wrong way? Obviously but HOW am I thinking wrong? I don't want to keep guessing I want to understand and be able to think the solution out the right way.
Have you tried to see if you could get this program to work at all the right way?
WTH is the >>>illegal XML character<<< anyways?
This was my last shot at the bottom section and I got this error:
Code:
Error: SyntaxError: illegal XML character
Line: 43, Column: 9
Source Code:
if (t=(<1&&>99)) 
--------^
Even on the comparison operator's page they have this:
Code:
14.if (hour<6 && hour>12) //&& logically compares truth of two values
15.document.write(“<h1>Good afternoon!!</h1>”);

Last edited by msNewbie; 09-09-2012 at 01:41 PM..
msNewbie is offline   Reply With Quote
Old 09-09-2012, 02:23 PM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
As Logic Ali points out there is an error before it gets to the if statement. You haven't created a function, although you've started one:

Code:
function  // complete this definition, or delete it, as appropriate
	if(t==.01 || x<1)
		{
			alert("Sorry! You lose! You have less than one dollar.")
		}
		
	else if(t > 1)    // should you be checking x or t?
		{
			alert("Sorry! You lose! You have more than one dollar.") 
		}
	
	else if (t == 1)
Once you've corrected all this you'll need to account for rounding errors. That is, t might end up as 1.00000001.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
msNewbie (09-10-2012)
Old 09-09-2012, 07:16 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
If rounding errors are an issue you can do

Code:
var x = 1.0000001;
x = x.toFixed(2);  // x is now a string value
x = Number (x);  // back to a number
alert (x);
__________________

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
Users who have thanked Philip M for this post:
msNewbie (09-10-2012)
Old 09-10-2012, 12:46 PM   PM User | #9
msNewbie
New Coder

 
Join Date: Sep 2012
Posts: 15
Thanks: 9
Thanked 0 Times in 0 Posts
msNewbie is an unknown quantity at this point
Quote:
Originally Posted by AndrewGSW View Post
As Logic Ali points out there is an error before it gets to the if statement. You haven't created a function, although you've started one:

Code:
function  // complete this definition, or delete it, as appropriate
	if(t==.01 || x<1)
		{
			alert("Sorry! You lose! You have less than one dollar.")
		}
		
	else if(t > 1)    // should you be checking x or t?
		{
			alert("Sorry! You lose! You have more than one dollar.") 
		}
	
	else if (t == 1)
Once you've corrected all this you'll need to account for rounding errors. That is, t might end up as 1.00000001.
==============
++++++++++++++
==============

If you look at the complete code above I took your advice.
The top section has been tested and works great.
This is the bottom section that refuses to work.
The function line has been removed.

Here is the change I made and I am STILL thinking wrong somehow.
I realize that I have only just started learning code for only two weeks but I feel that I am missing something even after reading the site someone gave me and this is due 9/11/12.
Code:
	if (t=(<1&&>99))
		{
			alert("Sorry! You lose! You have less than one dollar.");
		}
		
	if (t=(<1.01&&>50))
		{
			alert("Sorry! You lose! You have more than one dollar.");
		}
	
	if (t= (1))
		{
			alert("Congratulations! You WIN! You have exactly one dollar.");
		}
}
Has anyone tried to get this script to work on their computer?
msNewbie is offline   Reply With Quote
Old 09-10-2012, 01:21 PM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
if (t=(<1&&>99))
This is more wrong than the original code! You don't seem to have read or understood the advice, and code, given in previous posts.

Code:
var wibble = 50;
if (wibble >= 1 && wibble <= 100) {
    alert('Yes it is somewhere between 1 and 100!');
}
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW 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:02 AM.


Advertisement
Log in to turn off these ads.