Ask user for a number, and count from that number up to 300 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 &gt;= 150 &amp;&amp; num1 &lt;= 300){
  
     outNum(); 

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

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

 document.write("############################&lt;br&gt;"); 
 return true; 
}




Can you help me make this better (more efficient)?
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 &lt; 150) {
        alert("The number is too low for this function.");  

    } else if (input &gt; 300) {
        alert("The number is too high for this function.");

    } else {
        printNumbers(input);

    }

  }  // end of while loop

}

function printNumbers(number) {
  var output = "";

  for (; number &lt;= 300; number++) {
    output = output + number + "&lt;br&gt;"
  }

  output = output + "############################&lt;br&gt;"; 
  document.write(output); // Just change the document once
}

run();