Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function isWerewolf(target) {
- let word = target.toLowerCase().replace(/[,' '?!]/g,'');
- let reverse = '';
- for (let i = word.length - 1; i >= 0; i--) {
- reverse += word[i];
- }
- let result = word == reverse ? true : false;
- return result;
- }
- //second solution without regex
- function isWerewolf(target) {
- const letters = 'abcdefghijklmnopqrstuvwxyz';
- const lowerTarget = target.toLowerCase();
- let direct = '';
- let reversed = '';
- for (const ch of lowerTarget) {
- if (letters.includes(ch)) {
- direct += ch;
- reversed = ch + reversed;
- }
- }
- return direct === reversed;
- }
- /* Palindrome words
- Create the function that accepts the target string and returns true if it's a werewolf or false if it isn't.
- A werewolf is a word or sentence that you can read the same in both directions (left to right and vice versa) ignoring case, spaces, and punctuation.
- For example:
- isWerewolf('rotator'); // true ('rotator' -> 'rotator')
- isWerewolf('home'); // false ('home' -> 'emoh')
- isWerewolf('Racecar'); // true (case is ignored)
- isWerewolf('eva, can i see bees in a cave'); // true (spaces and punctuation are ignored)
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement