3
\$\begingroup\$

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){
		
    	outNum(); 

	}else if(num1 < 150){
		alert("The number is too low for this function."); 	
		num1 = 0; 
		conDition();	

	}else if(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 + "<br>");
    	num1++;
	}while (num1 <= 300);

	document.write("############################<br>");	
	return true; 
}

Can you help me make this better (more efficient)?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

I would avoid making 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 < 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();
\$\endgroup\$
3
  • \$\begingroup\$ for (; number <= 300; number++) <<< WHOA. this is legal? \$\endgroup\$ Mar 9, 2017 at 19:11
  • \$\begingroup\$ this is great . I had a feeling calling conDition() over and over was not helpful and it could be better. I was pretty happy I even got it to do the task. This is soooo helpful!!! \$\endgroup\$ Mar 9, 2017 at 19:16
  • \$\begingroup\$ Yes it is legal to leave out the initializer part, or any other part. It is also legal to have multiple parts for (; number <= 300; number++, q--) {. If it makes you more comfortable you can also make a more normal loop -> for (var i=number; i <= 300; i++) { ... \$\endgroup\$ Mar 9, 2017 at 22:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.