0

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)];
}
2
  • The same way you set any array element. Commented Oct 15, 2022 at 10:11
  • You are almost there. Instead of assigning the current element to middle, simply turn it around and assign the new value to the element! (So instead of let middle = stuff you have stuff = 'hi'.) Commented Oct 15, 2022 at 17:53

3 Answers 3

2

You can simply achieve this requirement by just finding the index of middle element and then assign the hi string to that index.

Live Demo :

function hiInTheMiddle(arry) {
  if(arry.length % 2 === 0) {
    return arry;
  }
  arry[Math.floor((arry.length - 1) / 2)] = 'hi';
  return arry;
}

const res = hiInTheMiddle([1, 2, 3, 4, 5]);

console.log(res);

Sign up to request clarification or add additional context in comments.

1 Comment

@CherryDT Thanks for pointing that out. I updated the answer. Hope it will work as per the expectation now.
1

You can simply change the middle element with arr[middle] = 'Hi'

So the code you need should be something like this:

let evenArr = [1, 2, 3, 4];
let oddArr = [1, 2, 3, 4, 5, 6, 7];
// defined test arrays

function hiInTheMiddle(arr) {
  if (arr.length % 2 === 0) {
    return arr;
  }
  
  arr[arr.length / 2 - 0.5] = 'Hi';
  return arr;
}

console.log(hiInTheMiddle(evenArr), hiInTheMiddle(oddArr));
// test the code

Comments

0

Details are commented in example

const odd = [1, 2, 3, 4, 5];
const even = [1, 2, 3, 4];

/**
 * If array has an odd numbered .length, replace the middle item of array 
 * with a given string.
 * @param {Array} arr  - An array of unknown values
 * @param {String} str - A string to insert into the array. 
 * @return {Array} - If undefined it returns @default as "|". 
 *         If arr .length is even, the array is returned untouched.
 */
const mid = (arr, str = "|") => {
  // If arr is odd...
  if (arr.length % 2 === 1) {
    // replace the middle item with the given string
    arr.splice((arr.length / 2), 1, str);
  }
  return arr;
}

console.log(JSON.stringify(mid(odd)));

console.log(JSON.stringify(mid(even)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.