mh108
1/31/2020 - 8:10 PM

function countUp

Example of using a while loop to generate a list of numbers

// jshint esversion: 7

//while loop examples: return list of numbers 1 .. n

function countUp(n){
  let i = 1, numList = "";
  
  //generate numbers 1 .. n
  while(i <= n){
    numList = numList + i + " ";
    i++;
  }
  
  return numList;
}

function countUp_with_backtick(n){
  let i = 1, numList = "";
  
  //generate numbers 1 .. n
  while(i <= n){
    numList = `${numList}${i} `;
    i++;
  }
  
  return numList;
}