forked from alexf1225/coderbyte_js_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcb_AlphabetSoup.js
More file actions
27 lines (22 loc) · 805 Bytes
/
Copy pathcb_AlphabetSoup.js
File metadata and controls
27 lines (22 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Using the JavaScript language, have the function AlphabetSoup(str) take the str string parameter being passed and return the string with the letters in alphabetical order (ie. hello becomes ehllo). Assume numbers and punctuation symbols will not be included in the string.
var sortFunction = function(str){
var arr = str.split(""),
sorting = true,
temp;
while (sorting){
sorting = false
for (var i = 0; i <= arr.length-2; i++){
if(arr[i+1] < arr[i]){
//code to swap positions
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
//flag to repeat if this swap happened
sorting = true;
}
}
}
return arr.join("")
};
var strToSort = "coderbyte";
console.log(sortFunction(strToSort)); //solution: "bcdeeorty"