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,449 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);

1 Answer

Steven Parker
Steven Parker
242,770 Points

For future reference, take a look at these videos about Posting a Question and using Markdown formatting to preserve the code's appearance. And for when your code is in a workspace, check out this one about sharing a snapshot of your workspace.

But mostly what makes code "fine" is if it performs the task it is designed to do, and this seems to fit the bill.   :+1:
Your arrow function definition is correct.

JavaScript also has a more compact version of the arrow function definition that you can optionally use when it only needs to return a calculated expression. It would look like this.

const getRandomNumber = (lower,upper) => Math.floor(Math.random() * (upper-lower+1)) + lower;
Maciek Radomski
seal-mask
.a{fill-rule:evenodd;}techdegree
Maciek Radomski
Python Development Techdegree Student 9,449 Points

I do have a question if I use

do {
    try {
        const variable1 = Number(prompt('Number 1'));
        const variable2 = Number(prompt('Number 2'));
        if (isNaN(variable1) || isNaN(variable2)) {
            throw new Error('Try again!');
        }
    } catch (err) {
        console.error(err)
    }
} while ( condition ) ;

can I use prompt in a loop like do ... while so when I get an error for the wrong input I can use loop to ask again the user for the input ?

I know prompt will not be used if at all but still I'm curious.

Steven Parker
Steven Parker
242,770 Points

It's true that prompt isn't commonly used in web aps, but there are other methods for getting input. And the use of do ... while for retries is indeed a common practice.