1

I want to write a function when entering ‘1’ it should print numbers between 1 to 11 when entering ‘2’ it should print numbers between 2 to 12 and so on. With not much success I am not able to figure out the logic as you can see in my below code. If someone can please help me to rectify.

function printNum() {
  var num = prompt("Enter a number between 1 to 5");
  for (var i = 1; i <= 11; i +=1) {
    console.log(i);
   }
  }
  printNum();
}

3
  • Does that mean when entering '3', it prints the numbers between 3 and 13? Commented Oct 10, 2022 at 1:38
  • you var num = ... then don't use num at all - perhaps you need to use the num entered? for (var i = 0; i <=10; i +=1) console.log(+num+i); note: the unary + is significant Commented Oct 10, 2022 at 1:54
  • thank you Jaromanda X for making me understand the logic Commented Oct 10, 2022 at 2:14

4 Answers 4

1

You've hard-coded the numbers 1 and 11 into your function, so your function will only print numbers within that range.

If you want to allow the user to specify the range, you'll need to replace those numbers with variables that will allow for accepting any input from the user. You already have this with the variable num.

But this also brings up another problem. What if the user doesn't enter a number? What if they enter a letter or a negative number? Or a decimal? How will you handle those possibilities?

In any case, to answer your original question, you'll need to replace the 1 and 11 in your for loop with the input that you accepted from the console.

Since the console only outputs a string, you will need to convert the string to an integer using parseInt()

Since all of your number ranges will be within a range of ten, i.e. 1 to 11 or 2 to 12, you can specify the end of your for loop as i <= num + 10.

function printNum() {
  var num = prompt("Enter a number between 1 to 5");
  num = parseInt(num);
  for (var i = num; i <= num + 10; i++) {
    console.log(i);
  }
}
printNum();

But again, this function still has some problems, notably, what if the user enters an unacceptable input? You'll need to check the user's input in that case.

Consider the following:

function printNum() {
  var responses = "12345"
  var num = prompt("Enter a number between 1 to 5");
  if (responses.includes(num)) {
    num = parseInt(num);
    for (var i = num; i <= num + 10; i++) {
      console.log(i);
    }
  } else {
    console.log("Error. Input must be in the range 1 to 5");
  }
}
printNum();

Some will further argue that a function named printNum() should only do one thing, print a number.

The function above does multiple things: it accepts user input, checks the input against a variable called responses, and either prints the result or an error message to the console.

One last possible solution is to break the functions apart into multiple functions so that each function performs a single task.

Consider the following:

function printNum(num) {
  for (var i = num; i <= num + 10; i++) {
    console.log(i);
  }
}

function isBetweenOneAndFive(varToCheck) {
  var responses = "12345";
  if (responses.includes(varToCheck)) {
    return true;
  } else {
    return false;
  }
}

var num = prompt("Enter a number between 1 to 5");
if (isBetweenOneAndFive(num)) {
  num = parseInt(num);
  printNum(num);
} else {
  console.log("Error. Input must be in the range 1 to 5");
}

The above example is more akin to modular programming, where the code is separated in such a way that it involves individual tasks per function.

Sign up to request clarification or add additional context in comments.

Comments

0

Is that what you need ?

function printNum() {
  var num = prompt("Enter a number between 1 to 5");
  let target = parseInt(num)+10;
  for (var i = 1; i <= target; i +=1) {
    console.log(i);
   }
  }
  printNum();

Comments

0

The number at the start of the sequence appears to be offset from 0 by the number entered, and the number at the end of the sequence appears to be offset from 10 in the same way.

Thus:

while (true) {
  const number = Number.parseInt(prompt('Enter a number between 1 and 5'));

  for (let currentNumber = number; currentNumber <= 10 + number; currentNumber++) {
    console.log(currentNumber);
  }
}

You don't need math skills to code but they appear to make it a bit easier.

Comments

0
function printNum() {
   const num = Number(prompt("Enter a number between 1 to 5"));
   if(num) {
     for(let i = num; i <= num + 10; i++){
        console.log(i);
     }
   }
}

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.