Jump to content

Sort Strings: Difference between revisions

From Drywall Wiki
Created page with "<h1>sort words in alphabetical order</h1> // take input const string = prompt('Enter a sentence: '); // converting to an array const words = string.split(' '); // sort the array elements words.sort(); // display the sorted words console.log('The sorted words are:'); for (const element of words) { console.log(element); }"
 
No edit summary
Line 1: Line 1:
<h1>sort words in alphabetical order</h1>
<h1>sort words in alphabetical order</h1>
// take input
*// take input
const string = prompt('Enter a sentence: ');
*const string = prompt('Enter a sentence: ');
// converting to an array
*// converting to an array
const words = string.split(' ');
*const words = string.split(' ');
// sort the array elements
*// sort the array elements
words.sort();
*words.sort();
// display the sorted words
*// display the sorted words
console.log('The sorted words are:');
*console.log('The sorted words are:');
for (const element of words) {
*for (const element of words) {
  console.log(element);
console.log(element);
}
}

Revision as of 17:46, 7 January 2026

sort words in alphabetical order

  • // take input
  • const string = prompt('Enter a sentence: ');
  • // converting to an array
  • const words = string.split(' ');
  • // sort the array elements
  • words.sort();
  • // display the sorted words
  • console.log('The sorted words are:');
  • for (const element of words) {
  • console.log(element);

}