Wrox Programmer Forums
|
BOOK: Beginning JavaScript 4th Edition
This is the forum to discuss the Wrox book Beginning JavaScript, 4th Edition by Paul Wilton, Jeremy McPeak; ISBN: 978-0-470-52593-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 4th Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2012, 08:59 PM
Authorized User
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Question [Chapter 3] Problem with do..while loop

I am having a problem getting my program to exit my do...while loop. I am also having a problem with my if statement. This program is my variation of the program in excercise 4 chapter 3. The code is below:

Code:
/*This program will ask the user for three numbers.  Two numbers are taken to be multiplied and  the third number is to tell the program how much times to muliply the number. */

function timesTable (start,secondNum,numEnd){

  var firstNum;
  for (firstNum = start; firstNum <= numEnd; firstNum++) {

  document.write (firstNum + " * " + secondNum + " = " + firstNum *   
  secondNum + "<br />" );

  }
}

do{

var a = parseInt (prompt("Enter the number you want to start your times table:", "")); 
var b = parseInt (prompt("Enter the number were you want your times table to end", ""));
var c = parseInt (prompt("Enter the first number you want to muliply by", ""));

   if (isNaN(a||b||c)==true){
     alert("You have entered an invalid key");
     alert("Please try again");
   }
  else 
     timesTable(a,c,b);
}

while (a||b||c != -1);
 
Old November 21st, 2012, 11:35 AM
jmcpeak's Avatar
Wrox Author
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
Default

Howdy, Truck35.

The problem boils down to the conditions in the if and while statements. Let's look at the if statement:

Code:
// original
if (isNaN(a||b||c)==true)
It's understandable why you wrote it this way. It reflects how we think as normal people: "If a, b, or c is not a number..." But programmers have to think in a different way: "If a is not a number, or b is not a number, or c is not a number...". That translates to the following in code:

Code:
// better
if (isNaN(a) == true || isNaN(b) == true || isNaN(c) == true)
But this code still needs work because it compares a boolean value with another boolean value--which is unnecessary in JavaScript (and most other languages). So the above could be rewritten as:

Code:
// correct
if (isNaN(a) || isNaN(b) || isNaN(c))
I'll let you apply this concept to the while statement. I'm here if you need help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 3, Loops and Logic. 'Calculating Primes' nest loop example p0stalDud3 BOOK: Ivor Horton's Beginning Java, Java 7 Edition 2 July 29th, 2012 05:32 PM
Chapter 2 - Iterator for loop example question craig9001 BOOK: Expert PHP and MySQL 0 July 8th, 2012 06:57 AM
Have a problem in For-each loop LeoMathew XSLT 2 July 16th, 2008 05:20 AM
loop problem smilesmita Pro PHP 1 November 2nd, 2007 02:15 AM
Loop problem! Please help me!! olud ASP.NET 1.0 and 1.1 Basics 2 March 19th, 2007 07:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.