function sortByLength(strs) { // write code here. // console.log(strs.sort((arg1, arg2) => arg1.length - arg2.length)); let...
function sortByLength(strs) {
// write code here.
// console.log(strs.sort((arg1, arg2) => arg1.length - arg2.length));
let swap;
for(let i = 0; i < 4; i++){
for( let j = 0; j < 4; j++){
if(strs[j].length > strs[j+1].length){
swap = strs[j];
strs[j] = strs[j+1];
strs[j+1] = swap;
}
}
}
// console.log(strs);
return strs;
}
/**
* 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"]);
});
});
COMMENTS