w3resource

JavaScript: Convert the letters of a given string in alphabetical order

JavaScript Basic: Exercise-52 with Solution

Write a JavaScript program to convert letters of a given string alphabetically.

Pictorial Presentation:

JavaScript: Convert the letters of a given string in alphabetical order

Sample Solution:

JavaScript Code:

// Define a function named alphabet_Soup with parameter str
function alphabet_Soup(str) { 
    // Split the string into an array of characters, sort the array, and join it back into a string
    return str.split("").sort().join("");
}

// Log the result of calling alphabet_Soup with the given strings to the console
console.log(alphabet_Soup("Python"));
console.log(alphabet_Soup("Exercises")); 

Sample Output:

Phnoty
Eceeirssx

Live Demo:

See the Pen JavaScript - string in alphabetical order - basic-ex-52 by w3resource (@w3resource) on CodePen.


Flowchart:

Flowchart: JavaScript - Convert the letters of a given string in alphabetical order

ES6 Version:

 // Define a function named alphabet_Soup with parameter str
const alphabet_Soup = (str) => {
    // Split the string into an array of characters, sort the array, and join the characters back into a string
    return str.split("").sort().join("");
};

// Log the result of calling alphabet_Soup with the given strings to the console
console.log(alphabet_Soup("Python"));
console.log(alphabet_Soup("Exercises"));

Previous: JavaScript program to convert a given number to hours and minutes.
Next: JavaScript program to check if the characters a and b are separated by exactly 3 places anywhere (at least once) in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.