From 52dfbe96dc796f4dea5dd9656292b7f9c5bf0520 Mon Sep 17 00:00:00 2001 From: Anna Date: Wed, 22 May 2024 05:14:32 -0400 Subject: [PATCH] fix(curriculum): prevent hardcoding palidrome checker answers (#54816) Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com> Co-authored-by: Krzysztof G. <60067306+gikf@users.noreply.github.com> --- .../build-a-palindrome-checker.md | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-palindrome-checker-project/build-a-palindrome-checker.md b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-palindrome-checker-project/build-a-palindrome-checker.md index f3da8d57b26..f108aaaa81e 100644 --- a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-palindrome-checker-project/build-a-palindrome-checker.md +++ b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-palindrome-checker-project/build-a-palindrome-checker.md @@ -241,6 +241,68 @@ checkBtn.click(); assert.strictEqual(resultEl.innerText.trim().replace(/[.,?!]+$/g, '').toLowerCase(), 'five|\_/|four is not a palindrome'); ``` +When the `#text-input` element contains an alphanumeric palindrome, the `#result` element should correctly identify it as a palindrome. + +```js +const inputEl = document.getElementById('text-input'); +const checkBtn = document.getElementById('check-btn'); +const resultEl = document.getElementById('result'); + +const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; +const charactersLength = characters.length; + +const firstLetter = characters.charAt(Math.floor(Math.random() * charactersLength)); +const secondLetter = characters.charAt(Math.floor(Math.random() * charactersLength)); +const thirdLetter = characters.charAt(Math.floor(Math.random() * charactersLength)); +const fourthLetter = characters.charAt(Math.floor(Math.random() * charactersLength)); + +const phrase = firstLetter + secondLetter + thirdLetter + fourthLetter + fourthLetter + thirdLetter + secondLetter + firstLetter; + +inputEl.value = phrase; +checkBtn.click(); + +assert.strictEqual(resultEl.innerText.trim().replace(/[.,?!]+$/g, '').toLowerCase(), phrase + ' is a palindrome'); +``` + +When the `#text-input` element contains a random sequence of alphanumeric characters that is not a palindrome, the `#result` element should say it is not a palindrome. + +```js +const inputEl = document.getElementById('text-input'); +const checkBtn = document.getElementById('check-btn'); +const resultEl = document.getElementById('result'); + +let characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; +let charactersLength = characters.length; + +const firstIndex = Math.floor(Math.random() * charactersLength); +const firstLetter = characters.charAt(firstIndex); +characters = characters.slice(0,firstIndex) + characters.slice(firstIndex + 1); +charactersLength--; + + +const secondIndex = Math.floor(Math.random() * charactersLength); +const secondLetter = characters.charAt(secondIndex); +characters = characters.slice(0,secondIndex) + characters.slice(secondIndex + 1); +charactersLength--; + +const thirdIndex = Math.floor(Math.random() * charactersLength); +const thirdLetter = characters.charAt(thirdIndex); +characters = characters.slice(0,thirdIndex) + characters.slice(thirdIndex + 1); +charactersLength--; + +const fourthIndex = Math.floor(Math.random() * charactersLength); +const fourthLetter = characters.charAt(fourthIndex); +characters = characters.slice(0,fourthIndex) + characters.slice(fourthIndex + 1); +charactersLength--; + +const phrase = firstLetter + secondLetter + thirdLetter + fourthLetter; + +inputEl.value = phrase; +checkBtn.click(); + +assert.strictEqual(resultEl.innerText.trim().replace(/[.,?!]+$/g, '').toLowerCase(), phrase + ' is not a palindrome'); +``` + # --seed-- ## --seed-contents--