Question: Write a function flat that flattens a two-dimensional array with 3 entries. let row1 = [4, 9, 2]; let row2 = [3, 5, 7]; let row3 = [8, 1, 6]; let loshu = [row1, row2, row3]; Example: flat(loshu) should return [4, 9, 2, 3, 5, 7, 8, 1, 6]. Thereby loshu is the magic square from the example above. Answer: function flat (array) { let entryA = array[0]; let entryB = array[1]; let entryC = array[2]; let finalD = entryA.concat(entryB, entryC); return finalD; }; > declare a function flat > it has 1 parameter array > we know that we need to flatten a two dimensional array which has 3 entries / arrays > an array that contains an array is called a two dimensional array > we declare 3 variables that access the three entries / arrays within the 2D array > we then concat all three arrays using the concat () method and store the output in another variable finalD > we return the finalD variable to get our single flattened array ...
Comments
Post a Comment