Last active
April 5, 2023 16:32
-
-
Save thesabbir/ad91620816a468e47efaf895bb2208b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const input1 = [1, 1, 2, 2, 3, 3, 8, 4, 4]; | |
const onlyOnce = (input) => { | |
const result = []; | |
input.filter((item) => { | |
let times = 0; | |
input.forEach((item2) => { | |
if (item === item2) { | |
times += 1; | |
} | |
}); | |
if (times === 1) { | |
result.push(item); | |
} | |
}); | |
return result[0]; | |
}; | |
console.log(onlyOnce(input1)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const input1 = "Hello world this is a test"; | |
const k1 = 3; | |
const truncate = (input, k) => { | |
const words = input.split(" "); | |
return words.filter((word, index) => index <= k - 1).join(" "); | |
}; | |
console.log(truncate(input1, k1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment