Created
October 2, 2015 04:54
-
-
Save kodamirmo/854bd56c5a30e904c774 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var array = ['a','b','c','d','e','f','g','h','i']; | |
var sort = function(array){ | |
var arrayA = []; | |
var arrayB = []; | |
var newArray = []; | |
var half = Math.floor((array.length/2)); | |
for(var i=0;i<array.length;i++){ | |
if(i<=half){ | |
arrayA.push(array[i]); | |
}else{ | |
arrayB.push(array[i]); | |
} | |
} | |
for(var i=0;i<arrayA.length;i++){ | |
newArray.push(arrayA[i]); | |
if(arrayB[i]){ | |
newArray.push(arrayB[i]); | |
} | |
} | |
return newArray; | |
}; | |
var printArray = function(array){ | |
for(var i=0;i<array.length;i=i+2){ | |
console.log(array[i],' ', array[i+1]?array[i+1]:''); | |
} | |
}; | |
printArray(sort(array)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment