Skip to content

Latest commit

History

History
180 lines (139 loc) · 4.93 KB

File metadata and controls

180 lines (139 loc) · 4.93 KB

Array

letarrayLength=5letarr1=[];letarr2=Array(arrayLength);// using Array object

length

console.log(arr1.length);console.log(arr2.length);

Adding data during execution

letarr3=['A',true,2];console.log(arr3[0]);console.log(arr3[1]);

Adding data after execution

arrayLength=2;letarr4=Array(arrayLength);arr4[0]='Value at index 0';console.log(arr4[0]);console.log(arr4[1])// undefined - no value added here 

length and index

letarr5=Array(3);arr5[2]='Adding a value';console.log(arr5[2]);console.log(arr5[arr5.length-1]);console.log(arr5[0]);// no value yet console.log(arr5[1]);// no value yet 

push() and pop()

letfellows=['Gabe','Fred','Mary'];fellows.push('Tim');console.log(fellows);// [ 'Gabe', 'Fred', 'Mary', 'Tim' ]console.log(`The length of fellows is ${fellows.length}`);// 4 console.log(fellows.pop())// Timconsole.log(fellows);

shift() and unshift()

console.log(fellows.unshift('Esther'));// add new value to the front of the arrayconsole.log(fellows);// [ 'Esther', 'Gabe', 'Fred', 'Mary' ]console.log(fellows.shift());// remove first valueconsole.log(fellows);// [ 'Gabe', 'Fred', 'Mary' ]

concat

letfoodOptions=['Thai','Seafood','Pasta'];letotherFoodOptions=['Tacos','Falafel'];letallFoodOptions=foodOptions.concat(otherFoodOptions);console.log(`All the food options ${allFoodOptions}`);// All the food options Thai,Seafood,Pasta,Tacos,Falafel

Challenges

Challenge 1

Write a range function that takes two arguments, start and end, and returns an array containing all the numbers from start up to (and including) end.

Next, write a sum function that takes an array of numbers and returns the sum of these numbers. Run the example program and see whether it does indeed return 55.

As a bonus assignment, modify your range function to take an optional third argument that indicates the “step” value used when building the array. If no step is given, the elements go up by increments of one, corresponding to the old behavior. The function call range(1, 10, 2) should return [1, 3, 5, 7, 9]. Make sure it also works with negative step values so that range(5, 2, -1) produces [5, 4, 3, 2].

// Your code here.console.log(range(1,10));// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]console.log(range(5,2,-1));// → [5, 4, 3, 2]console.log(sum(range(1,10)));// → 55
Solution
functionrange(start,end,stepValue=1){letarr=[];if(stepValue<0&&start>end){for(letindex=start;index>=end;index-=Math.abs(stepValue)){arr.push(index);}}elseif(stepValue>0&&start<end){for(letindex=start;index<=end;index+=stepValue){arr.push(index);}}returnarr;}functionsum(numbers){letresult=0;for(constnumofnumbers){result+=num;}returnresult;}console.log(range(1,10));// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]console.log(range(5,2,-1));// [ 5, 4, 3, 2 ]console.log(sum(range(1,10)));// 55

Challenge 2

Arrays have a reverse method that changes the array by inverting the order in which its elements appear. For this exercise, write two functions, reverseArray and reverseArrayInPlace. The first, reverseArray, takes an array as argument and produces a new array that has the same elements in the inverse order. The second, reverseArrayInPlace, does what the reverse method does: it modifies the array given as argument by reversing its elements. Neither may use the standard reverse method.

Thinking back to the notes about side effects and pure functions in the previous chapter, which variant do you expect to be useful in more situations? Which one runs faster?

// Your code here.console.log(reverseArray(["A","B","C"]));// → ["C", "B", "A"];letarrayValue=[1,2,3,4,5];reverseArrayInPlace(arrayValue);console.log(arrayValue);// → [5, 4, 3, 2, 1]
Solution
functionreverseArray(inputArr){letoutputArr=[];for(constindexininputArr){outputArr.unshift(inputArr[index]);}returnoutputArr;}console.log(reverseArray(["A","B","C"]));// [ 'C', 'B', 'A' ]functionreverseArrayInPlace(inputArr){for(letindex=0;index<inputArr.length/2;index++){consttemp=inputArr[index];inputArr[index]=inputArr[inputArr.length-1-index];inputArr[inputArr.length-1-index]=temp;}}letarrayValue=[1,2,3,4,5];reverseArrayInPlace(arrayValue);console.log(arrayValue);// [ 5, 4, 3, 2, 1 ]

Resources

  1. MDN - Array