Advertisement
CoineTre

Palindrome

Oct 9th, 2024 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.21 KB | Source Code | 0 0
  1. function isWerewolf(target) {
  2.     let word = target.toLowerCase().replace(/[,' '?!]/g,'');
  3.      let reverse = '';
  4.      for (let i = word.length - 1; i >= 0; i--) {
  5.          reverse += word[i];
  6.      }
  7.      let result = word == reverse ? true : false;
  8.      return result;
  9.  }
  10.  
  11.  //second solution without regex
  12.  
  13.  function isWerewolf(target) {
  14.     const letters = 'abcdefghijklmnopqrstuvwxyz';
  15.     const lowerTarget = target.toLowerCase();
  16.     let direct = '';
  17.     let reversed = '';
  18.  
  19.     for (const ch of lowerTarget) {
  20.       if (letters.includes(ch)) {
  21.         direct += ch;
  22.         reversed = ch + reversed;
  23.       }
  24.     }
  25.  
  26.     return direct === reversed;
  27.   }
  28.  
  29. /* Palindrome words
  30. Create the function that accepts the target string and returns true if it's a werewolf or false if it isn't.
  31.  
  32. 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.
  33.  
  34. For example:
  35.  
  36. isWerewolf('rotator'); // true ('rotator' -> 'rotator')
  37. isWerewolf('home'); // false ('home' -> 'emoh')
  38. isWerewolf('Racecar'); // true (case is ignored)
  39. isWerewolf('eva, can i see bees in a cave'); // true (spaces and punctuation are ignored)
  40. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement