The problem is that in your string, the ⛵️
is two separate codepoints: the sailboat emoji (U+26F5) and a variation selector (U+FE0F). Your unicodeArray
has a length of 4, leading to more substrings.
If you omit the variation selector, it works as selected:
const s1 = "abc"
const s2 = "⛵️" // length 6
const s3 = "⛵" // length 5
console.log(s2 === s3) // false
function substrings(s) {
const unicodeArray = Array.from(s)
const result = []
for (let l = 1; l <= unicodeArray.length; l++) {
for (let i = 0; i <= unicodeArray.length - l; i++) {
result.push(unicodeArray.slice(i, i + l).join(''))
}
}
return result
}
console.log(substrings(s1)) // ["a", "b", "c", "ab", "bc", "abc"]
console.log(substrings(s2)) // ["", "⛵", "️", "", "⛵", "⛵️", "️", "⛵️", "⛵️", "⛵️"]
console.log(substrings(s3)) // ["", "⛵", "", "⛵", "⛵️", "⛵️"]