I am still pretty new to programing and have been working on challenges. One of them is creating a function that will:
-> Change the middle value in an odd array to a string "Hi" -> Return an even array as it is (no changes)
I have managed to find the middle element in the array with no problem (also setting up the condition for even arrays).
But I am struggling to figure out how to actually replace the element with a string and return the array.
For example [1, 2, 3, 4, 5] -> [1, 2, 'hi", 4, 5]
If anyone can point me in the right direction I would be very grateful. Thank you all for your help
function hiInTheMiddle(arry) {
if(arry.length % 2 === 0) {
return arry
}
let middle = arry[Math.floor((arry.length - 1) / 2)];
}
middle, simply turn it around and assign the new value to the element! (So instead oflet middle = stuffyou havestuff = 'hi'.)