This is my first attempt
const input = require("fs").readFileSync("/dev/stdin").toString().split("\n");
const count = Number(input[0]); // 13
// 1. shortest word.length
// 2. If the length is the same, in alphabetical order
// sort() ?
let newArr = [];
// insert all elements in array
let shortest = input[1].length; // just initial value
for (let i = 1; i <= count; i++) {
newArr.push(input[i]);
}
// remove duplicated elements in array
let removedArr = newArr.filter((item, index) => newArr.indexOf(item) === index);
removedArr.sort((a, b) => {
return a.length - b.length;
});
console.log(removedArr);
i cannot get the correct answer as my answer keeps mixing the order like
[ 'i', 'no', 'it', 'im', 'but', 'wont', 'more', 'wait', 'yours', 'cannot', 'hesitate' ]
After referring,
const input = require("fs").readFileSync("/dev/stdin").toString().split("\n");
const count = Number(input[0]); // 13
// 1. shortest word.length
// 2. If the length is the same, in alphabetical order
// sort() ?
let newArr = [];
// insert all elements in array
let shortest = input[1].length; // just initial value
for (let i = 1; i <= count; i++) {
newArr.push(input[i]);
}
// remove duplicated elements in array
let removedArr = newArr.filter((item, index) => newArr.indexOf(item) === index);
removedArr.sort((a, b) => {
return a.length - b.length || a.localeCompare(b);
});
console.log(removedArr.join("\n"));
Things i was missing
- I did't know the localeCompare(). This method returns a number indicating. By using 'localeCompare', can sort out in alphabetical order

Comments
Post a Comment