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.
var num =... then don't usenumat all - perhaps you need to use thenumentered?for (var i = 0; i <=10; i +=1) console.log(+num+i);note: the unary+is significantJaromanda Xfor making me understand the logic