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) {
// Bubble, in place
let sorted = false
let j = strs.length - 1
while (!sorted) {
sorted = true
for (let i = 0; i < j; i++) {
console.log(`${i} ${j}`)
if (strs[i].length > strs[i + 1].length) {
let temp = strs[i + 1]
strs[i + 1] = strs[i]
strs[i] = temp
sorted = false
}
console.log(strs)
}
j--
}
return strs

// Sort with callback, copy
// myStr = strs.slice()
// myStr.sort((a, b) => a.length < b.length ? -1 : 1)
// return myStr
}



/**
* 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
"0 4"
,
[
""
,
"abc"
,
"aaa"
,
"a"
,
"zz"
]
,
"1 4"
,
[
""
,
"abc"
,
"aaa"
,
"a"
,
"zz"
]
,
"2 4"
,
[
""
,
"abc"
,
"a"
,
"aaa"
,
"zz"
]
,
"3 4"
,
[
""
,
"abc"
,
"a"
,
"zz"
,
"aaa"
]
,
"0 3"
,
[
""
,
"abc"
,
"a"
,
"zz"
,
"aaa"
]
,
"1 3"
,
[
""
,
"a"
,
"abc"
,
"zz"
,
"aaa"
]
,
"2 3"
,
[
""
,
"a"
,
"zz"
,
"abc"
,
"aaa"
]
,
"0 2"
,
[
""
,
"a"
,
"zz"
,
"abc"
,
"aaa"
]
,
"1 2"
,
[
""
,
"a"
,
"zz"
,
"abc"
,
"aaa"
]
,
"result: "
,
[
""
,
"a"
,
"zz"
,
"abc"
,
"aaa"
]
,
/index.html
LIVE