scrimba
Solution for day 6 of #javascriptmas
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Solution for day 6 of #javascriptmas
AboutCommentsNotes
Solution for day 6 of #javascriptmas
Expand for more info
main.js
run
preview
console
function sortByLength(strs) {
let sorted = [];
while(strs.length > 0)
{
let number = 0;
for(i = 0; i < strs.length; i++)
{
if(strs[number].length > strs[i].length)
{
number = i;
}
}

sorted.push(strs[number])
strs.splice(number,1);
}
return sorted;
}



/**
* Test Suite
*/
describe('sortByLength()', () => {
it('sorts the strings from shortest to longest', () => {
// arrange
const strs = ["abc", "", "aaa", "a", "zz"];

// act
const result = sortByLength(strs);

// log
console.log("result: ", result);

// assert
expect(result).toEqual(["", "a", "zz", "abc", "aaa"]);
});
});
Console
"result: "
,
[
""
,
"a"
,
"zz"
,
"abc"
,
"aaa"
]
,
/index.html?spec=sortByLength()
LIVE