• This is a reminder of 3 IMPORTANT RULES:

    1- External self-promotion websites or apps are NOT allowed here, like Discord/Twitter/Patreon/etc.

    2- Do NOT post in other languages. English-only.

    3- Crack/Warez/Piracy talk is NOT allowed.

    Breaking any of the above rules will result in your messages being deleted and you will be banned upon repetition.

    Please, stop by this thread SoccerGaming Forum Rules And Guidelines and make sure you read and understand our policies.

    Thank you!

Javascript Help

hiryuu.com

Senior Squad
I have a problem...
I can seem to make a while function work :S

What I need is to validate the name, as long as the user inputs something everything's ok, but if the user inputs nothing, then I need a new prompt asking the user to enter his/her name.

(I know it's easier with an if statement, but the teacher wants it in a While, or do while form, and I don't get it (probably, because I haven't been going to class :D).

Anyways, here's my code so far, which is wrong of course:

Code:
var name = prompt("What is your name?", "")

   	while(name==null)
   	{
   	name = prompt("Please, tell us your name, so we can serve you better:", "")
   	}

Right not it validates it the first time, but if they don't enter a name on the second prompt the loop stops :S
 

LOCOlombia

Starting XI
well, don't you want the prompt loop to stop once the name is entered, and for it to continue with the rest of the program. Let me see if I'm understading correctly, you have the loop set up now that once the name is entered correctly at the first prompt it will skip over the second part of the loop? that's how you have it set up now right?
 

hiryuu.com

Senior Squad
Let me try to explain it :P

When you load the page, it asks you for your name, if you enter a name you go to the next question, if not then it will say: "Please, tell us your name, so we can serve you better:" as on the code above.

Now, if the user enters his name there, he goes to the next question.

That's fine untill I press cancel or I don't input a name on that second prompt, and it STILL takes me to the next question.
 

hiryuu.com

Senior Squad
Here is the whole HTML code (well only the javascript part, everything else I took out):

Code:
<html>
<head>
 <title>A3P1: Manuel R. Medina</title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
<!--
var mex = 65
var bra = 78
var quan = 100
var name = prompt("What is your name?", "")

   	while(name= "")
   	{
   	name = prompt("Please, tell us your name, so we can serve you better:", "")
   	}

var mex2 = parseInt(prompt(name + ", how many Mexico\'s jerseys do you want?", ""))

	while (isNaN(mex2) || mex2 > quan) {
	mex2 = parseInt(prompt(name + ", please enter a valid quantity (from 0 to 100).\nHow many Mexico\'s jerseys do you want?", ""))
	}

var bra2 = parseInt(prompt(name + ", how many Brazil\'s jerseys do you want?", ""))

	while (isNaN(bra2) || bra2 > quan) {
	bra2 = parseInt(prompt(name + ", please enter a valid quantity (from 0 to 100).\nHow many Brazil\'s jerseys do you want?", ""))
	}

var sum1 = mex2 * mex
var sum2 = bra2 * bra
var total = sum1 + sum2
	document.write("<hr>")
	document.write(mex2 + " Mexico\'s jersey\(s\) @ $" + mex + " =" + " $" + sum1)
	document.write("<br>")
	document.write(bra2 + " Brazil\'s jersey\(s\) @ $" + bra + " =" + " $" + sum2)
	document.write("<br>")
	document.write("Total Amount = $" + total)
	document.write("<br>")

   if(mex2 ==0 && bra2 ==0)
   {
      document.write("Thank you anyway for considering our products!");
   }
   else if(mex2 >= 1 && bra2 >= 1)
   {
      document.write("We hope you like our quality jerseys!");
   }
   else if(mex2 >= 1 || bra2 >= 1)
   {
      document.write("Next time try our other jerseys!");
   }
   if((mex2 + bra2) >= 10)
   {
      document.write("<br><b><font color=\"red\">Your order will be shipped for free!</font></b>");
   }
-->
</script>
</body>
</html>
 

hiryuu.com

Senior Squad
So far, I got it "working", at least now if the user press cancel it will ask again for the name (but the whole loop is a mess :S).

Code:
	do
   	{
	var name = prompt("What is your name?", "")
	if(name==null)
	{	
   	name = prompt("Please, tell us your name, so we can serve you better:", "")
	}
   	} while(name==null)

Now if you go to this page: http://www.academic.rccd.cc.ca.us/~mcleod/js/class_demos/loops/do_while.html you will see a WORKING do while loop, and the code is this one:

Code:
   var input, age1;

   do 
   {
      input = prompt("Enter your age for entry to a NC-17 movie:", "");
      age1 = parseInt(input);
   
      if(isNaN(age1) || age1 < 0 || age1 > 120)
      {
         input = prompt(input + " is not a valid age! Enter your age for a NC-17 movie again:", "");
         age1 = parseInt(input);
      }

   }while(isNaN(age1) || age1 < 0 || age1 > 120)

I hope that way you could "translate" this working code to what I need, THANKS in advance.
 

LOCOlombia

Starting XI
Originally posted by hiryuu.com
So far, I got it "working", at least now if the user press cancel it will ask again for the name (but the whole loop is a mess :S).

Code:
	do
   	{
	var name = prompt("What is your name?", "")
	if(name==null)
	{	
   	name = prompt("Please, tell us your name, so we can serve you better:", "")
	}
   	} while(name==null)

Now if you go to this page: http://www.academic.rccd.cc.ca.us/~mcleod/js/class_demos/loops/do_while.html you will see a WORKING do while loop, and the code is this one:

Code:
   var input, age1;

   do 
   {
      input = prompt("Enter your age for entry to a NC-17 movie:", "");
      age1 = parseInt(input);
   
      if(isNaN(age1) || age1 < 0 || age1 > 120)
      {
         input = prompt(input + " is not a valid age! Enter your age for a NC-17 movie again:", "");
         age1 = parseInt(input);
      }

   }while(isNaN(age1) || age1 < 0 || age1 > 120)

I hope that way you could "translate" this working code to what I need, THANKS in advance.

I think I have it, its like a while loop I found in my C++ book, it goes:

do
{
it prompts user to enter age by ?
age1= input by user

if(age1 = < 0 or > 120)
{
it will prompt yuser (to enter a real age again);
age1 = user's input
}
}while (age1 < 0 > 120) repeats whole loop again.

so...

Code:
var input, name;

do
{
  input = prompt("question you want?")
  name = parseInt(input);

 if(isNaN(name) == null)
   {
    input = prompt(input + "your question again?:'','''');
    name = parseInt(input);
    {
 }while(isNan(name) == null)

your problem one the first code you posted here might have been the fact that you haden't declared the variables, you declared name as the variable but withing the body of loop, so as it wasn't expecting anything to be fill in the place of the "input" being the name, then it didn't require anything for it to go to the next question.

Now, make sure you have a second copy of your original work (code) in case this new modifications don't work.
 

hiryuu.com

Senior Squad
Tried, won't work :(

I will try to make some changes, as I think you're on the right track, altought we don't have to use "IsNaN" on this case, since of course the input won't be a number (and if it was it's ok, I just need to check to see if it's blank > ask the question again).
 

monkee

Senior Squad
right mate, here it is:

When you are dealing with comparisons that return true or false (boolean expressions) you need to double up your =. So where you've put:

while(name="")

it should be

while(name=="") ...
 

hiryuu.com

Senior Squad
Originally posted by monkee
right mate, here it is:

When you are dealing with comparisons that return true or false (boolean expressions) you need to double up your =. So where you've put:



it should be

while(name=="") ...

Well duh :confused:

HAHAHAHAHAH

THANK YOU GUYS you saved my day, thank God it's due today, so I won't lose points...

THANK YOU Again, I just tested it and it works like a charm... here's the "fixed" code:

Code:
var name = prompt("What is your name?", "")

	 while(name=="")
	{	
   	name = prompt("Please, tell us your name, so we can serve you better:", "")
	}

THANK YOU :D
 

SnYper

Senior Squad
I'm doing first year Comp Sci (sucks, so i'm changing majors). Anyway we're finishing off java and logic as well, and I thought I would add one thing to what someone mentioned.

== is the symbol for equivalence, which in boolean is a 3 line equal sign.:) ;)

Hiryuu, i've run into the same problem a few times, it sucks how it's usually something so simple that is messing up your code.
 

hiryuu.com

Senior Squad
Originally posted by SnYper
I'm doing first year Comp Sci (sucks, so i'm changing majors). Anyway we're finishing off java and logic as well, and I thought I would add one thing to what someone mentioned.

== is the symbol for equivalence, which in boolean is a 3 line equal sign.:) ;)

Hiryuu, i've run into the same problem a few times, it sucks how it's usually something so simple that is messing up your code.

yep, the problem is ALWAYS some stupid little line, or character :crazyboy:
 

LOCOlombia

Starting XI
yup, haha it funny when compiling a source code, all the erros that sometimes are caused by one little misused character like you said...:p
 

SnYper

Senior Squad
I was checking the replies and something came to mind that I always do to avoid that exact problem you have. When I'm comparing strings I use the x.equals("") because it'll just check the string value of the variable. Easier for me to read and understand atleast:D It should work in javascript:p


It'd look something like this:

Code:
var name = prompt("What is your name?", "")

	 while(name.equals(""))
	{	
   	name = prompt("Please, tell us your name, so we can serve you better:", "")
	}
 


Top