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-08-2013, 03:15 AM   PM User | #1
wise4aa
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
wise4aa is an unknown quantity at this point
I cannot find the bug.

I wrote this little code to put a few skills together to make sure I am understanding the logic correctly.
*Code runs a prompt asking for your name
*Take the name and runs a prompt to ask your age.
*I have loops set up to make sure the fields are filled out correctly, if not it re-runs the prompt.
*The prompt to ask the age, keeps looping, I have the parameters set correctly it looks like, but it won't accept any number to end the loop.

**I emboldened the few lines of code I am having an issue with.

Thanks,
Russell

Code:
var counter= 0
//declares vaule to variable
var yourName= prompt("What is your name?")
//variable created from prompt
	while(counter= 1){
//loop to run to ensure prompt is filled out
		if (yourName.length== 0) {
//condtion to check if variable has a value
	var yourName= prompt("What is your name?") 
	counter =1}
//counter set to re-run the loop if variable is blank
	

		else {
//run the new prompt once variable has a value 
		counter= 2		
		var age= prompt(yourName+ " how old are you?")} 
}
//declares value to new variable 


var counter2= 0 
	while(counter2 = 1) {
//loop to verify content of prompt is correctly filled out
		if (age <0 || age >99) {
//conditional to set parameters for the loop
		var age= prompt(yourName+ " how old are you?") 
		 counter =1} 
//sets variable counter to 1 to re-run loop
		else {
//sets counter to 2 to end loop and print a string
		 counter= 2
	document.write("Hello "+ yourName+ " I see you are"+ age+ " years old.")	
}
wise4aa is offline   Reply With Quote
Old 02-08-2013, 03:28 AM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
didnt read it all, but while(counter2 = 1) should def be while(counter2 == 1)
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 02-08-2013, 04:05 AM   PM User | #3
wise4aa
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
wise4aa is an unknown quantity at this point
update

I did some more tinkering around with it, and I found the bug is in a different area of the code (new emboldened lines)

Code:
var counter= 0; 
//declares value to variable
var yourName= prompt("What is your name?")
//variable created from prompt
	while(counter= 1){
//loop to run to ensure prompt is filled out
		if (yourName.length== 0) {
//condition to check if variable has a value
	var yourName= prompt("What is your name?") 
	counter= 1}
//counter set to re-run the loop if variable is blank
	

		else {
//run the new prompt once variable has a value 
		counter= 2		
		var age= prompt(yourName+ " how old are you?")} 
}
//declares value to new variable 


var counter2= 0 
	while(counter2 == 1) {
		if (age <0 || age >99) {
		var age= prompt(yourName+ " how old are you?") 
		 counter =1} 
		else {
		 counter= 2
	document.write("Hello "+ yourName+ " I see you are"+ age+ " years old.")	
} 
	 	}
wise4aa is offline   Reply With Quote
Old 02-08-2013, 04:28 AM   PM User | #4
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
Code:
while(counter= 1){
probably isn't doing you any favours, either
xelawho is offline   Reply With Quote
Old 02-08-2013, 05:31 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You shouldn't be using either prompt or document.write in your javaScript - those became obsolete a long time ago.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-08-2013, 07:26 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by felgall View Post
You shouldn't be using either prompt or document.write in your javaScript - those became obsolete a long time ago.
Absolutely right!

But if you insist on using prompt, here is the best way to require a valid entry-


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

for (var i=1; i<2; i++) {
var ans = prompt ("Enter your name here", "");
if ((ans == null ) || (ans == "") || (/[^a-z\s\-\']/gi.test(ans))) {
alert ("You must enter your name using only letters, space, hyphen and/or apostrophe!! ");
i -- ;
}
}

</script>
Likewise to require a number in range 0-100

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

var repeat = true;
while(repeat) {
var ans = parseFloat(prompt("Enter a positive number max.100",""));
if ((isNaN(ans)) || (ans == null) || (ans < 0) || (ans > 100)) {
alert ("You must enter a positive number max. 100");
}
else {
repeat = false;
}
}

</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.

Last edited by Philip M; 02-08-2013 at 07:28 AM..
Philip M is offline   Reply With Quote
Old 02-08-2013, 07:14 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Slightly simpler:
Code:
while( true ) 
{
    var ans = parseFloat(prompt("Enter a positive number max.100",""));
    if ((isNaN(ans)) || (ans == null) || (ans < 0) || (ans > 100)) 
    {
        alert ("You must enter a positive number max. 100");
    } else { break; }
}
Or, my preference (I don't know why...just me):
Code:
while( true ) 
{
    var age = parseInt(prompt("Enter a positive whole number max.100",""));
    if ( ! isNaN(age) && age != null && age >= 0 && age <= 100 ) { break; }
    alert ("You must enter a positive whole number max. 100");
}
I did change to parseInt since this is supposed to be getting an age.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-08-2013, 07:25 PM   PM User | #8
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Of course none of those approaches will prevent a visitor using any browser except Internet Explorer from simply checking the checkbox at the bottom of the prompt either on its first appearance in Opera - where it turns off JavaScript - or on its second appearance in other browsers - where it turns off all further prompts, confirms and alerts.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-10-2013, 09:35 PM   PM User | #9
wise4aa
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
wise4aa is an unknown quantity at this point
it runs

Thanks for the feedback yall, using values of true/false/null helped it run along with my mistakes of = instead of ==.

Code:
<html>
<script>
function nameFunction(){
var counter= 1;
//declares value to variable
var yourName= prompt("What is your name?", "Enter your name")
//variable created from prompt
	while(counter== 1){
//loop to run to ensure prompt is filled out
		if (yourName.length== 0 || yourName==="Enter your name" || yourName== null) {
//condition to check if variable has a value
	var yourName= prompt("What is your name?") 
	(counter)}
//counter set to re-run the loop if variable is blank
			else {
//run the new prompt once variable has a value 
		(counter++)		
		var age= prompt(yourName+ " how old are you?", "Enter your age")} 
}
//declares value to new variable 
var counter2= 1
//sets a new variable to the counter
	while(counter2 == 1) {
		//counter to check new prompt 
		if (age <=0 || age >99) {
			//parameters for new prompt
		var age= prompt(yourName+ " how old are you?") 
		 counter2 =1} 
		else {
			//variable to end loop
		(counter2++)
	document.write("Hello "+ yourName+ ", I see you are "+ age+ " years old.")	
	//prints statement based on variable
} 
	 } 
}
</script>
<body>
	<input type= "button" onclick="nameFunction()" value="Click me">
	<script>
	//links function to a button 
	</script>
</body>


</html>
wise4aa 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 02:57 PM.


Advertisement
Log in to turn off these ads.