You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
vararrayBuilder=function(size){// create an empty array to store our valuesvararray=[];// iterate the value of the argumentfor(vari=0;i<size;i++){// append the value of i to our arrayarray.push(i);}returnarray;}console.log(arrayBuilder(4));//=> [0, 1, 2, 3]
Migrate building an array that doubles the index
vardoubleTheIndex=function(size){// create an empty array to store our valuesvararray=[];// iterate the value of the argumentfor(vari=0;i<size;i++){// append double the value of i to our arrayarray.push(i*2);}returnarray;}console.log(doubleTheIndex(4));//=> [0, 2, 4, 6]
Migrate building an array whose odd indicies are double and even indicies quadruple
functionoddOrEvenResults(size){// create an empty array to store our valuesvararray=[];// all odd indices are doubled while all even indices are quadrupledfor(vari=0;i<size;i++){// use javascript's remainder operator (is there any remainder when the index is divided by 2?)if(i%2===0){// quadruple the value of the index if evenarray[array.length]=i*4;}elseif(i%2===1){// double the value of the index if oddarray[array.length]=i*2;}}returnarray;}console.log(oddOrEvenResults(6));//=> [0, 2, 8, 6, 16, 10]
Migrate building an array of all of the whole numbers between two integers inclusive
varinclusiveRange=function(lowerBound,upperBound){// calculate the distance between the higher and lower valuesvardistance=upperBound-lowerBound;// create an empty array to store our valuesvarrange=[];// iterate the value of distance plus 1for(vari=0;i<=distance;i++){// append the value of the expression to the array using the array's length propertyrange[range.length]=lowerBound+i;}returnrange;}console.log(inclusiveRange(0,5);//=> [0, 1, 2, 3, 4, 5]// even better and more abstract handles any two numbers in any ordervarinclusiveRange=function(num1,num2){// initialize variables to range boundariesvarlowerBound,upperBound;// determine the larger and smaller number if(num1>num2){upperBound=num1;lowerBound=num2;}else{upperBound=num2;lowerBound=num1;}// calculate the distance between the higher and lower valuesvardistance=upperBound-lowerBound;// create an empty array to store our valuesvarrange=[];// iterate the value of distance plus 1for(vari=0;i<=distance;i++){// append the value of the expression to the array using the array's length propertyrange[range.length]=lowerBound+i;}returnrange;}console.log(inclusiveRange(5,0);//=> [0, 1, 2, 3, 4, 5]
Migrate finding the mean (average) value of the elements within an array
varaverageValue=function(array){varsum=0;// iterate over each element of the arrayfor(vari=0;i<array.length;i++){// reassign the value of sum to the current value of sum plus the value of the array elementsum=sum+array[i];}// the mean value is the total sum of the array elements divided by the number of elementsreturnsum/array.length;}