Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

random simple personal project

This is my first time ever asking on a question so, I hope I don't break any rules or have bad "neticate".

anyway, I gave myself a problem to solve and I have been working on it all day. The answer may be simple, but I just can't figure it out.

I just wanted to create a program that askes for a number, then verifies that the input is indeed a number. If the input is a number, I want it to 'print' the number with the alert(); function. But, if the input isn't a number, I want to chastise them for not choosing a number then execute the code until I get a number.

I have tried everything I can think of, and the only thing I can get to work is this:

do { var num = Number(prompt('pick a number')); } while (isNaN(num));

and all this does is keep asking the question until it receives a number. I have tried making it a function and calling it in a if/then statement, and tried to use it in a do while statement, and I am not smart enough to do it with a for statement.

Any help or advice at all would be great.

thanks.

3 Answers

Try this for a function:

function isNumber(newNumber)
{
  var result = true;

  if (isNaN(newNumber))
  {
    result = false;
  }

  return result;
}

Mike just one suggestion, i wouldnt use the curly brackets like that :) better way is:

function something() {
}

http://encosia.com/in-javascript-curly-brace-placement-matters-an-example/

I have always used the Allman-style of braces ... There are indeed certain situations, like in the article you linked, that would require for the K&L-style braces, but I never return objects that way. I would declare the object variable first, then return it. I know it may be a bit more than needed, but I find it makes my code more readable, and I don't run into edge-cases like that.

Of course this is personal preference, I find Allman-style code much easier to read. I don't think either way is right or wrong.

Thanks for the help with seeing it as a function. I have truly been struggling with this simple task. Looks like I still have a long way to go.

Sorry, for the delay I was at work. So, I see the new function and I can understand what it is doing, I think(er...), but do I still incorporate it into a do/while loop?

do { 
var num = Number(prompt('pick a number'));
if(isNaN(num)){
console.log("You are being chastised!");
}
 } while (isNaN(num));

Thanks a lot I don't know why I didn't think to just use the if statement by itself.