Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Kyle Jensen
Courses Plus Student 6,293 PointsUp for a challenge??? Any ideas on this function?
So, I've been working on Codewars challenges and the MDN for the past week, or so. I've been able to answer a lot of challenges and have even taken to recreating some Javascript methods. I've written my own reverse function, well two of them. One reverses all the letters of a given string and the other reverses the order of the words. To the point, I have been trying to figure out how to write my own split function that works like the js .split() method does, but I cannot seem to come up with a way to make an array of single characters like you would get by using yourString.split('');. I have spent some time trying, but at this point, I'm just hacking away at it and not able to come up with any real logic to it. All the other possibilities work, just not the one... Does anyone have any ideas? Here's what I have so far:
function splits(string, pos) {
var length = string.length;
var newString = '';
var newArray = [];
for(var i = 0; i < length; i++) {
var currenChar = string[i];
if(isNaN(string)) {
if(pos === null) {
newArray.push(currenChar); //make the string a single array item - same as: string.split()
}
else if(pos === '') {
//I can't seem to make an array of single characters - same as: string.split('')
}
else if(i < string.indexOf(pos)) { //make each word an item in an array - same as: string.split(' ')
newString += currenChar;
}
else if(currenChar === pos) {
newArray.push(newString);
newString = '';
}
else if(i > string.indexOf(pos) && i < length) {
newString += currenChar;
if(i === length - 1) {
newArray.push(newString);
}
}
}
}
return isNaN(string) ? newArray : new Error('splits(string) is not a function');
}
var hello = 'Hello Instagram, how are you today?';
var num = 10;
console.log(splits(hello));
console.log(splits(hello, ''));
console.log(splits(hello, ' '));
console.log(splits(hello, 'a'));
console.log(splits(num));
1 Answer

Steven Parker
242,191 PointsYou have a syntax error in that section.
You have a reference to isNaN but you did not supply an argument. But it doesn't seem to be needed anyway, this would appear to be all the job requires:
else if (pos === '') {
//I can't seem to make an array of single characters - same as: string.split('')
newArray.push(currenChar);
}
I'm not sure what your intention is for numbers, that part still needs some work.
Kyle Jensen
Courses Plus Student 6,293 PointsKyle Jensen
Courses Plus Student 6,293 PointsSteven Parker you're right about isNaN in the else if statement, it was extraneous, especially since the entire logic block is nested in an if statement that handles checking for isNaN. I've also changed the lower return statement section to the conditional operator to make it more concise. I see that removing the condition and adding the block you supplied works. It's funny, I had that in there at one point, but I guess the browser was trying to fix my isNaN statement thus negating the code I had