Reputation: 13
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();
}
Upvotes: 1
Views: 1194
Reputation: 404
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.
Upvotes: 1
Reputation: 50
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);
}
}
}
Upvotes: 0
Reputation: 9202
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.
Upvotes: 0
Reputation: 343
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();
Upvotes: 0