Skip to content

Instantly share code, notes, and snippets.

Created April 12, 2017 05:29
Show Gist options
  • Select an option

  • Save anonymous/a32deddce6c6da2402a62393dd03628f to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/a32deddce6c6da2402a62393dd03628f to your computer and use it in GitHub Desktop.
5.4 My Join created by smillaraaq - https://repl.it/HEVr/3
function myJoin(arr,sep) {
let str = "";
if (!sep) { sep = ","; }
for (let i = 0 ; i < arr.length ; i ++) {
if (arr[i] === undefined||arr[i] === null) {
continue;
}
if (!!str.length) {
str = str + sep;
}
str = str + arr[i];
}
return str;
}
function myJoin(arr,sep) {
let str = "";
if (!sep) {
sep = ",";
}
for (let i = 0 ; i < arr.length ; i ++) {
if (arr[i] === undefined||arr[i] === null) {
continue;
}else if (!str.length) {
str = str + arr[i];
} else {
str = str + sep;
str = str + arr[i];
}
}
return str;
}
console.log(myJoin([null,undefined, 'hello', undefined, 'world', undefined], ' '));
// => 'hello world'
console.log(myJoin([2, "be", false]));
// => '2,be,false`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment