Magical Palindrome¶
[!hint] in JS everything you touch acts like an object
So:
- This function may receive not only a string, but also a crafted object/payload
where properties (like
palindrome["length"]|palindrome.length|palindrome["0"]|palindrome[0](works because key "0" becomes number 0)|palindrome["99"]|palindrome[99]) are still accessible.
Code explanation:
const IsPalinDrome = (string) => {
if (string.length < 1000) {
return "Tootus Shortus";
}
console.log(
`1. 'Array().keys()' make it iterable: ${Array(string.length).keys()}`,
);
console.log("But passed only one object.");
console.log(
`2. Still have access to first field (length): ${Array(string.length)}`,
);
console.log(`3. second "0" field in object: ${string[0]}`);
console.log(`4. first field in object [ '1000' ]: ${string.length}`);
for (const i of Array(string.length).keys()) {
console.log("There is no iterations.");
const original = string[i];
console.log(`${i} Key: ${original}`);
const reverse = string[string.length - i - 1];
console.log(`string[string.length - i - 1]`);
console.log(`string[1000 - 0 - 1]`);
console.log(`${string.length - i - 1} Key: ${reverse}`);
if (original !== reverse || typeof original !== "string") {
return "Notter Palindromer!!";
}
}
return "Getus Flagus!";
};
console.log(IsPalinDrome({ length: "1000", 0: "Key", 999: "Key" }));