Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

while loop with sentinel value. 1

Status
Not open for further replies.

chrisgarvey

Technical User
Mar 27, 2003
64
GB
Hello,

I am trying to practice my skills as i am a beginner.

I have wrote the following program below to double a number.

I am now trying to create a while loop with a sentinel value.
When the user types eof, i am trying to get the program to stop prompting to enter a number and instead display program ended.

Any help would be much appreciated!

Chris

<HTML>
<HEAD>

<TITLE>Double it</TITLE>

</HEAD>
<BODY>

<H1>Doubller</H1>
<script language=&quot;JavaScript&quot;>

//ask the use for a number and try to convert the input into a number
var userNumber = Number(prompt(&quot;Enter a number between 1 and 10&quot;,&quot;&quot;));

//sentinel value of eof to terminate loop
while (value != eof);
{

//if the value of userNumber is NaN then ask the user to try again
if (isNaN(userNumber))
{
alert(&quot;Please ensure a valid number is entered&quot;);
window.location.reload();

}

//if the value is a number but over 10 then ask the user to try again
else
{
if (userNumber > 10 || userNumber < 1)
{

alert (&quot;The number you entered is not between 1 and 10&quot;);
window.location.reload();
}

//otherwise the number is between 1 and 10 so write to the screen
else
{
alert(&quot;The number you entered was &quot; + userNumber);
alert(&quot;The number doublled is &quot; + userNumber * '2');
window.location.reload();
}
}
}
//loop ended print program ended
document.write(&quot;program ended&quot;);
</script>
</BODY>
</HTML>

 
Try this:
Code:
   // variable for done;
   var isDone = false;
   // main loop
   while (!isDone) {
      // ask the use for a number and try to
      // convert the input into a number
      var userNumber = Number(window.prompt(&quot;Enter a number between 1 and 10&quot;,&quot;&quot;));
      if (isNaN(userNumber)) {
         // not a number
         window.alert(&quot;Please ensure a valid number is entered&quot;);
      } else if ((userNumber > 10) || (userNumber < 1)) {
         // number out of range
         window.alert &quot;The number you entered is not between 1 and 10&quot;);
      } else {
         // all ok   
         window.alert(&quot;The number you entered was &quot; + userNumber);
         window.alert(&quot;The number doublled is &quot; + userNumber);
         isDone = true;
      }
   }
   // loop ended print program ended
   document.write(&quot;program ended&quot;);

That should do it.

The [tt]reload[/tt] you used just causes the whole thing to start again.

[tt]________________________________________________________________
[pc2]Roger
Life is a game of cards in which the deck contains only jokers.[/tt]
 
When I try & run the program in Interent Exploer, nothing happens!

I have added of course

<HTML>
<script language=&quot;JavaScript&quot;>

code....

</script>
</HTML>

This is confusing me, anyone know why?

 
There were a couple of typos in my post. Sorry.

The following really does work:
Code:
<html>
   <head>
   </head>
   <body>
   <script language=&quot;JavaScript&quot;>
      // variable for done;
      var isDone = false;
      // main loop
      while (!isDone) {
         // ask the use for a number and try to
         // convert the input into a number
         var userNumber = Number(window.prompt(&quot;Enter a number between 1 and 10&quot;,&quot;&quot;));
         if (isNaN(userNumber)) {
            // not a number
            window.alert(&quot;Please ensure a valid number is entered&quot;);
         } else if ((userNumber > 10) || (userNumber < 1)) {
            // number out of range
            window.alert(&quot;The number you entered is not between 1 and 10&quot;);
         } else {
            // all ok   
            window.alert(&quot;The number you entered was &quot; + userNumber);
            window.alert(&quot;The number doublled is &quot; + (userNumber * 2));
            isDone = true;
         }
      }
      // loop ended print program ended
      document.write(&quot;program ended&quot;);
   </script>
   </body>
</html>

[tt]________________________________________________________________
[pc2]Roger
Life is a game of cards in which the deck contains only jokers.[/tt]
 
Curious why you used a loop it's not needed. Try the amended code below.
Code:
<html>
   <head>
   </head>
   <body>
   <script language=&quot;JavaScript&quot;>
         // ask the use for a number and try to
         // convert the input into a number
         var userNumber = Number(window.prompt(&quot;Enter a number between 1 and 10&quot;,&quot;&quot;));
         if (isNaN(userNumber)) {
            // not a number
            window.alert(&quot;Please ensure a valid number is entered&quot;);
         } else if ((userNumber > 10) || (userNumber < 1)) {
            // number out of range
            window.alert(&quot;The number you entered is not between 1 and 10&quot;);
         } else {
            // all ok   
            window.alert(&quot;The number you entered was &quot; + userNumber);
            window.alert(&quot;The number doubled is &quot; + (userNumber * 2));
                }

      //  print program ended
      document.write(&quot;program ended&quot;);
   </script>
   </body>
</html>

Glen
 

Glen,

I think the idea was that many sets of numbers could be entered until the user wanted to quit.

Just my interpretation of the post ;o)

Dan
 
Yes Dan,

Thats what I am working on now.

I would be intrested to here any ways to achieve this.

Chris
 

>> I would be intrested to here any ways to achieve this.

Did Woja's post not work for you, then?

Dan
 
Not quite sure what you need, (oviously by my last post) but you can put as many prompts as you need in the loop. Hope I'm closer on this post.
Code:
<html>
   <head>
   </head>
   <body>
   <script language=&quot;JavaScript&quot;>
      // variable for done;
      var isDone = false;
      // main loop
      while (!isDone) {
         // ask the use for a number and try to
         // convert the input into a number
         var userNumber = Number(window.prompt(&quot;Enter a number between 1 and 10&quot;,&quot;&quot;));
         if (isNaN(userNumber)) {
            // not a number
            window.alert(&quot;Please ensure a valid number is entered&quot;);
         } else if ((userNumber > 10) || (userNumber < 1)) {
            // number out of range
            window.alert(&quot;The number you entered is not between 1 and 10&quot;);
         } else {
            // all ok   
            window.alert(&quot;The number you entered was &quot; + userNumber);
            window.alert(&quot;The number doubled is &quot; + (userNumber * 2));
var userNumber1 = Number(window.prompt(&quot;Enter a second number between 1 and 10&quot;,&quot;&quot;));
         if (isNaN(userNumber1)) {
            // not a number
            window.alert(&quot;Please ensure a valid number is entered&quot;);
         } else if ((userNumber1 > 10) || (userNumber1 < 1)) {
            // number out of range
            window.alert(&quot;The second number you entered is not between 1 and 10&quot;);
         } else {
            // all ok   
            window.alert(&quot;The second number you entered was &quot; + userNumber1);
            window.alert(&quot;The second number doubled is &quot; + (userNumber1 * 2));
            isDone = true;
         }
      }
      // loop ended print program ended
}      document.write(&quot;program ended&quot;);
   </script>
   </body>

Glen
 
I wasn't trying to get extra prompts.

Just the ability to type 'exit' at the command prompt to exit the program.

Hope you see what i am getting at?

Thanks for your help.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top