Repository navigation
#237 Fix the Broken Code
A student learning JavaScript was trying to make a function that sorts all the letters of a word,
but their code is broken, and they can't figure out why.
Spot and fix the error(s) in the code to make the function work.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/237 Fix the Broken Code.js"Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.
This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.
//A student learning JavaScript was trying to make a function that sorts all the letters of a word,
//but their code is broken, and they can't figure out why.
//Spot and fix the error(s) in the code to make the function work.
function sortWord(word) {
return word.split('').sort().join('');
}
import { assertEquals } from '../testHelper.js';
assertEquals(sortWord("dcba"), "abcd")
assertEquals(sortWord("pneumonoultramicroscopicsilicovolcanoconiosis"), "aacccccceiiiiiilllmmnnnnooooooooopprrsssstuuv")
assertEquals(sortWord("Unpredictable"), "Uabcdeeilnprt", "Capital letters should come first!")
assertEquals(sortWord("a"), "a")
assertEquals(sortWord("ability"), "abiilty")
assertEquals(sortWord("able"), "abel")
assertEquals(sortWord("about"), "abotu")
assertEquals(sortWord("above"), "abeov")
assertEquals(sortWord("accept"), "accept")