My splice method don't work here (don't give my code attention just I want to know why this code don't work in my console).
function capSpace(txt) {
// write your code here
wordSplit = txt.split("");
for (let i = 0; i < wordSplit.length; i++) {
if (wordSplit[i].toUpperCase() == wordSplit[i]) {
wordSplit.splice(5, 0, " ")
}
}
return wordSplit
}
console.log(capSpace("fausJkalMalkihkLhb"));
First of all, you need to increment the i for each character that you add to the array.
I assume you're trying to add a space before each capital letter, in which case you need to add it at the i index, not the 5th index.
function capSpace(txt) {
// write your code here
wordSplit = txt.split("");
for (let i = 0; i < wordSplit.length; i++) {
if (wordSplit[i].toUpperCase() == wordSplit[i]) {
wordSplit.splice(i, 0, " ");
i++;
}
}
return wordSplit
}
var result = capSpace("fausJkalMalkihkLhb").join('');
console.log(result);
The splice method is adding an empty space to the wordSplit array in your snippet found here:
wordSplit.splice(5, 0, " ")
This is incrementing the length of wordSplit on each iteration and the condition of the for loop:
i < wordSplit.length
will never be satisfied because the length of wordSplit increases which creates an infinite loop and therefore is never able to return wordSplit and therefore never able to console.log the variable of result because it's stuck in an infinite loop.