fixTheMeerkat Kata

reverse an array

by trentHarlem

JavaScript

const reverseArray = (arr) => arr.reverse()
//----.reverse() alters the Original array----------------




// this method would NOT change the original array
function fixTheMeerkat(arr) {
  let fix = []
  arr.map(i => fix.unshift(i))
console.log('fix', fix)
  console.log('arr', arr) 
  return fix
}



// this method would NOT change the original array
/* function fixTheMeerkat(arr) {
  let fix = []
  arr.forEach(i => fix.unshift(i))
  console.log('fix', fix)
  console.log('arr', arr)
  return fix
} */

//-----------------------------


/* function fixTheMeerkat(arr) {
let temp = []
for (let i=0; arr.length>0;i++) {
temp.push(arr.pop())
}
return temp
}
 */
//-----------------------------

/* function fixTheMeerkat(arr) {
let temp = []
for (let i=0; i<arr.length;i++) {
temp.push(arr.pop())
}
temp.push(arr.shift())
return temp
} */

//-----------------------------
/* function fixTheMeerkat(arr) {
let count = arr.length
let temp = []
for (let i=0; i<count;i++) {
temp.push(arr.pop())
}
return temp
} */


//return arr.map(i => arr[2],arr[1],arr[0])

//[2, 1, 0]

console.log(fixTheMeerkat(["tail", "body", "head"]), ["head", "body", "tail"]);
//console.log(fixTheMeerkat(["tails", "body", "heads"]), ["heads", "body", "tails"]);
//console.log(fixTheMeerkat(["bottom", "middle", "top"]), ["top", "middle", "bottom"]);
//console.log(fixTheMeerkat(["lower legs", "torso", "upper legs"]), ["upper legs", "torso", "lower legs"]);
console.log(reverseArray(["ground", "rainbow", "sky"]), ["sky", "rainbow", "ground"])