0

I'm trying to make a function that asks the user for a number in between whatever number is passed to the functions min and max, eg.(1,10) I can't seem to get it to work though, what am I missing/ doing wrong here?

function getProductChoice(min, max) {

    do {
        var productIndex = parseInt(prompt('Enter your product choice', '0'));
    } while( isNaN(productIndex) || productIndex <= max || productIndex >= min);

    getProductChoice(1,6);
 };
1
  • 1
    Your while condition should contain "bad" outcomes. For min and max it doesn't. Plus move function call outside of the function. Right now you have endless recursion. Commented Jan 5, 2015 at 4:39

1 Answer 1

1

I'm assuming you want to stop prompting when the given number satisifies the range. However, your current code does the opposite, continuing to run when the productIndex is less than the max or greater than the min. Try switching your max and min in the conditional.

In this example I've also pulled the getProductChoice() function call out of the function, as recursion is not necessary.

function getProductChoice(min, max) {
    do {
        var productIndex = parseInt(prompt('Enter your product choice', '0'));
    } while( isNaN(productIndex) || productIndex <= min || productIndex >= max);
 };
 getProductChoice(1,6);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.