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 JavaScript Basics (Retired) Creating Reusable Code with Functions Random Number Challenge

Kirby Abogaa
Kirby Abogaa
3,058 Points

Feedback please...

I took this challenge and i would like some feedback if there was anything i could have done better in my code...

Please do not include loops or functions not yet discussed up to when this challenge was given.

Thanks a lot guys!

Here is my code:

<script>

function number(min, max) {

  var rndNUmber = Math.floor( Math.random() * (max - min + 1) ) + min;

var guess = prompt("Can you guess the number?");
var message1 = "You are right! The number is indeed: " + rndNUmber;
var message2 = "Sorry! The number is: " + rndNUmber;

  if(parseInt(guess) === rndNUmber) {
      alert(message1);
  }else{
      alert(message2);
  }

}

var lowNum = parseInt( prompt("PLease enter a number.") );

var highNum = parseInt( prompt("PLease enter a number higher than the first.") );

number(lowNum, highNum);

</script>

2 Answers

Tony Nguyen
Tony Nguyen
24,934 Points

Looks good to me, when you're doing camel casing, you should make sure that it capitalizes on each word.

For example you did:

var rndNUmber;

A good practice is to do it on each iteration of a word like this:

var rndNumber;

Also indentation and spaces is important to improve readability and for us, for example you coded:

function number(min, max) {

  var rndNUmber = Math.floor( Math.random() * (max - min + 1) ) + min;

var guess = prompt("Can you guess the number?");
var message1 = "You are right! The number is indeed: " + rndNUmber;
var message2 = "Sorry! The number is: " + rndNUmber;

  if(parseInt(guess) === rndNUmber) {
      alert(message1);
  }else{
      alert(message2);
  }

}

With proper indentation:

function number(min, max) {

  var rndNumber = Math.floor( Math.random() * (max - min + 1) ) + min;

  var guess = prompt("Can you guess the number?");
  var message1 = "You are right! The number is indeed: " + rndNumber;
  var message2 = "Sorry! The number is: " + rndNumber;

  if (parseInt(guess) === rndNumber) {
      alert(message1);
  } else {
      alert(message2);
  }

}

Very good job, by the way!

Kirby Abogaa
Kirby Abogaa
3,058 Points

Hey Tony!

Thank you for your time and feedback. Really appreciate it!

Regarding my camel casing, seems I'm pressing shift one character too long. Check my lowNum and highNum prompt message - "PLease ...". Didn't notice that till now. LOL!

Cheers!