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 Functions Arrow Functions Testing for Number Arguments

Maciek Radomski
seal-mask
.a{fill-rule:evenodd;}techdegree
Maciek Radomski
Python Development Techdegree Student 9,239 Points

Is this solution fine ?

I would like to ask if this solution is fine because I kind of wanted to make it python-like with lambda-like with arrow function but not sure if this is ok for JavaScript or if I can use something like this and probably my comments aren't the best but well still want to ask the question.

code: /**

  • Returns a message to a page
  • @param {string} message - the message for the user to see.
  • @return {string} - message with HTML code like <h1></h1> for better formatting. */

function writeToPage(message) { const selector = document.querySelector('main'); return selector.innerHTML = ${message} }

/**

  • Checks if the Input is a number and returns a message based on if it passes if statement.
  • @param {*} lowerNumber - user input of lower number to convert to Int and check.
  • @param {*} upperNumber - user input of upper number to convert to Int and check.
  • @function getRandomNumber - Arrow function returns a random number provided by the user.
  • @function writeToPage - function call to write the message to a web page. */

function checkInput(lowerNumber, upperNumber) { if (parseInt(lowerNumber) && parseInt(upperNumber)) { // should use isNaN() const getRandomNumber = (lower, upper) => { return Math.floor(Math.random() * (upper - lower + 1)) + lower }; writeToPage(<h1>The randomized number between ${lowerNumber} and ${upperNumber}</br>is: ${getRandomNumber(parseInt(lowerNumber), parseInt(upperNumber))}</h1>); } else { writeToPage(<h1>Please provide numbers!</br>Your inputs:</br>lower number: ${lowerNumber}</br>upper number: ${upperNumber}</h1> </br></br><h2>Please reload a page and try again!<h2>) } }

// Call the function and pass it different values

const lowerNumber = prompt("What is the lower number to randomize?"); const upperNumber = prompt("What is the upper number to randomize?");

checkInput(lowerNumber, upperNumber);