I've done similar exercises to this one before and always figured it out, but I'm stuck with the silliest thing. I'm doing the classic "get the middle character' (This function takes a string and returns an array containing the middle characters of each word. If the word is of even length then you should find the middle two characters) and I think my code should work well for all tests but the empty string one. When an empty string "" is passed, it should return an empty array [], an even specifying to do so, it returns "".
This is my code so far:
function getMiddleChars (str) {
let empty =[];
let middle = str.length /2;
for(let i =0; i<str.length; i++){
if(str === ""){
return empty;
}else if(str.length %2 == 0){
return str.slice(middle -1, middle +1);
}
}
return str.charAt(middle);
}
returns [] when passed "":
AssertionError: expected '' to deeply equal []
Could anyone give me a hand? Thanks in advance.
EDITED:
function getMiddleChars (str) {
let solution = [];
let middle = str.length /2;
if(str === ""){
return [];
} if(str.length %2 === 0){
return solution.push(str.slice(middle -1, middle +1));
} else {
return solution.push(str.charAt(middle));
}
}
Still won't work. I was pretty sure I could figure this out and I'm lost now.
if (str === "") return [];return emptyand goes straight toreturn str.charAt(middle). Middle is 0, so the character at 0 is an empty string''.