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
Darin Thompson
5,305 Pointsrandom 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
Mike Bronner
16,395 PointsTry this for a function:
function isNumber(newNumber)
{
var result = true;
if (isNaN(newNumber))
{
result = false;
}
return result;
}
Darin Thompson
5,305 PointsSorry, 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?
Charles Massry
12,253 Pointsdo {
var num = Number(prompt('pick a number'));
if(isNaN(num)){
console.log("You are being chastised!");
}
} while (isNaN(num));
Darin Thompson
5,305 PointsThanks a lot I don't know why I didn't think to just use the if statement by itself.
Sadi Qevani
311 PointsSadi Qevani
311 PointsMike just one suggestion, i wouldnt use the curly brackets like that :) better way is:
http://encosia.com/in-javascript-curly-brace-placement-matters-an-example/
Mike Bronner
16,395 PointsMike Bronner
16,395 PointsI 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.
Mike Bronner
16,395 PointsMike Bronner
16,395 PointsOf course this is personal preference, I find Allman-style code much easier to read. I don't think either way is right or wrong.
Darin Thompson
5,305 PointsDarin Thompson
5,305 PointsThanks 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.