javascript - Sorting strings from prompt is not working but it works when hard coded -


my code not working when give value via prompt working hard coded input.

could please me understand why is?

var str=prompt("enter string :: "); alert(selectionsort(str));  function selectionsort(items){     var len = items.length,         min;      (i=0; < len; i++){         min = i;          //check rest of array see if smaller         (j=i+1; j < len; j++){             if (items[j] < items[min]){                 min = j;             }         }         if (i != min){             swap(items, i, min);         }     }     return items; } 

this swapping function:

function swap(items, firstindex, secondindex){     var temp = items[firstindex];     items[firstindex] = items[secondindex];     items[secondindex] = temp; } 

if want sort characters can use:

var str = "farhan"; var sorted = str.split("").sort().join(""); console.log(sorted); // "aafhnr" 

.split("") turns string array. .sort sorts array (default ascending order) , .join("") turns array string.

for educational purposes might useful write own sort routine, beyond that, use built in functions as can.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -