RosettaCodeData/Task/Determine-if-a-string-has-all-the-same-characters/JavaScript/determine-if-a-string-has-all-the-same-characters-1.js
2023-07-01 13:44:08 -04:00

15 lines
527 B
JavaScript

const check = s => {
const arr = [...s];
const at = arr.findIndex(
(v, i) => i === 0 ? false : v !== arr[i - 1]
)
const l = arr.length;
const ok = at === -1;
const p = ok ? "" : at + 1;
const v = ok ? "" : arr[at];
const vs = v === "" ? v : `"${v}"`
const h = ok ? "" : `0x${v.codePointAt(0).toString(16)}`;
console.log(`"${s}" => Length:${l}\tSame:${ok}\tPos:${p}\tChar:${vs}\tHex:${h}`)
}
['', ' ', '2', '333', '.55', 'tttTTT', '4444 444k', '🐶🐶🐺🐶', '🎄🎄🎄🎄'].forEach(check)