I have to use a.splice(index, 0, value) to solve.
this is what I have so far, but the value for final is an empty array(i initialized to null for debugging purposes); I think the problem is with my splice, because my output is just an empty array but i'm pretty sure my syntax is correct.
Input
a == [7, 10, 15, 54]
x = 99
Output:
[7, 10, 99, 15, 54]
function solution(a, x) {
let count = a.length+1
let mid = count/2
let final = null
if (mid % 2 === 0) {
final = a.splice(a[mid], 0, x)
} else {
let middle = mid - 1
final = a.splice(a[middle], 0, x)
}
return final
}
Edit:
I see the comment and have amended the code to this:
let count = a.length+1
let mid = count/2
if (mid % 2 === 0) {
a.splice(mid, 0, x)
} else if (mid % 2 == 1) {
let middle = mid-1
a.splice(middle, 0, x)
}
return a
}
but this input fails because it doesn't want to insert the new value?
**Input:
a: [64, 38, 22, 27, 62, 41] x: 90
Output:
[64, 38, 22, 27, 62, 41]
Expected Output:
[64, 38, 22, 90, 27, 62, 41]**