patternjavascriptMinor
Ask user for a number, and count from that number up to 300
Viewed 0 times
300numberuseraskthatforandcountfrom
Problem
I'm learning Javascript and created the below to take input from a user. If the number is between 150 and 300 it will print out the numbers from that number up till and including 300 in sequential order.
`var num1;
conDition();
function conDition(){
num1 = prompt("Please enter a number: ");
if(num1 >= 150 && num1 300){
alert("The number is too high for this function.");
num1 = 0;
conDition();
}else{
alert("That is not a number!");
num1 = 0;
conDition();
}
}
function outNum(){
do
{
document.write(num1 + "
");
num1++;
}while (num1
Can you help me make this better (more efficient)?
`var num1;
conDition();
function conDition(){
num1 = prompt("Please enter a number: ");
if(num1 >= 150 && num1 300){
alert("The number is too high for this function.");
num1 = 0;
conDition();
}else{
alert("That is not a number!");
num1 = 0;
conDition();
}
}
function outNum(){
do
{
document.write(num1 + "
");
num1++;
}while (num1
Can you help me make this better (more efficient)?
Solution
I would avoid making
Instead of
Use more meaningful variable names.
This is a minor point for a learning exercise, but if efficiency is your goal try to reduce the number of times you modify the DOM (use
Lastly, though it is a somewhat personal preference and there is no hard and fast rule, I dislike using recursion to solve this type of input problem.
I would write this something like:
num1 a top level variable. Instead make it a local and pass it around. You also don't need to clear num1 every time. It will be cleared by the prompt method.var num1 = prompt("Please enter a number: ");
outNum(num1)
function outNum(num1) {
...Instead of
do..while I would recommend a for loop. Use more meaningful variable names.
This is a minor point for a learning exercise, but if efficiency is your goal try to reduce the number of times you modify the DOM (use
document.write or anything similar s it is relatively expensive.Lastly, though it is a somewhat personal preference and there is no hard and fast rule, I dislike using recursion to solve this type of input problem.
I would write this something like:
function run() {
while (true) { // this loop runs forever
var input = prompt("Please enter a number or press enter to exit: ");
if (!input)
break; // leave the loop
input = parseInt(input)
if (isNaN(input)) {
alert("That is not a number!");
} else if (input 300) {
alert("The number is too high for this function.");
} else {
printNumbers(input);
}
} // end of while loop
}
function printNumbers(number) {
var output = "";
for (; number "
}
output = output + "############################";
document.write(output); // Just change the document once
}
run();Code Snippets
var num1 = prompt("Please enter a number: ");
outNum(num1)
function outNum(num1) {
...function run() {
while (true) { // this loop runs forever
var input = prompt("Please enter a number or press enter to exit: ");
if (!input)
break; // leave the loop
input = parseInt(input)
if (isNaN(input)) {
alert("That is not a number!");
} else if (input < 150) {
alert("The number is too low for this function.");
} else if (input > 300) {
alert("The number is too high for this function.");
} else {
printNumbers(input);
}
} // end of while loop
}
function printNumbers(number) {
var output = "";
for (; number <= 300; number++) {
output = output + number + "<br>"
}
output = output + "############################<br>";
document.write(output); // Just change the document once
}
run();Context
StackExchange Code Review Q#157355, answer score: 4
Revisions (0)
No revisions yet.