jackd5011

Bad Code Coding Challenge #40 - To T9 Button Presses

Jul 6th, 2020
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // for context: https://www.reddit.com/r/badcode/comments/hm7ifj/bad_code_coding_challenge_40_to_t9_button_presses/
  2. function to_t9(str) {
  3.     let output = "";
  4.     let firstAllCapsTest = "";
  5.     for(let i = 0; i < str.length; i++) {
  6.         if(str.charAt(i) == str.charAt(i).toUpperCase()) {
  7.             firstAllCapsTest += str.charAt(i);
  8.         }
  9.     }
  10.     let wholeWordUppercase = firstAllCapsTest == str;
  11.     if(wholeWordUppercase) {
  12.         output += "## ";
  13.     }
  14.     str = str.split(" ");
  15.     function getKey(char) {
  16.         char = char.toLowerCase();
  17.         if(char == "0") return "00";
  18.         if(char == "1") return "11111";
  19.         if(char == "2") return "2222";
  20.         if(char == "3") return "3333";
  21.         if(char == "4") return "4444";
  22.         if(char == "5") return "5555";
  23.         if(char == "6") return "6666";
  24.         if(char == "7") return "77777";
  25.         if(char == "8") return "8888";
  26.         if(char == "9") return "99999";
  27.         if(char == "a") return "2";
  28.         if(char == "b") return "22";
  29.         if(char == "c") return "222";
  30.         if(char == "d") return "3";
  31.         if(char == "e") return "33";
  32.         if(char == "f") return "333";
  33.         if(char == "g") return "4";
  34.         if(char == "h") return "44";
  35.         if(char == "i") return "444";
  36.         if(char == "j") return "5";
  37.         if(char == "k") return "55";
  38.         if(char == "l") return "555";
  39.         if(char == "m") return "6";
  40.         if(char == "n") return "66";
  41.         if(char == "o") return "666";
  42.         if(char == "p") return "7";
  43.         if(char == "q") return "77";
  44.         if(char == "r") return "777";
  45.         if(char == "s") return "7777";
  46.         if(char == "t") return "8";
  47.         if(char == "u") return "88";
  48.         if(char == "v") return "888";
  49.         if(char == "w") return "9";
  50.         if(char == "x") return "99";
  51.         if(char == "y") return "999";
  52.         if(char == "z") return "9999";
  53.         if(char == ".") return "1";
  54.         if(char == ",") return "11";
  55.         if(char == "?") return "111";
  56.         if(char == "!") return "1111";
  57.         if(char == "'") return "*";
  58.         if(char == "-") return "**";
  59.         if(char == "+") return "***";
  60.         if(char == "=") return "****";
  61.         return "i did a fucky";
  62.     }
  63.     for(let word of str) {
  64.         let allCapsTest = "";
  65.         for(let i = 0; i < word.length; i++) {
  66.             if(word.charAt(i) == word.charAt(i).toUpperCase() && isNaN(parseInt(word.charAt(i)))) {
  67.                 allCapsTest += word.charAt(i);
  68.             }
  69.         }
  70.         let allCaps = false;
  71.         if(allCapsTest == word) {
  72.             allCaps = true;
  73.         }
  74.         if(allCaps && !wholeWordUppercase) {
  75.             if(word.length > 1) output += "## ";
  76.             else output += "# ";
  77.         }
  78.         for(let i = 0; i < word.length; i++) {
  79.             let c = word.charAt(i);
  80.             let key = getKey(c);
  81.             if(c == c.toUpperCase() && isNaN(parseInt(c)) && !allCaps && !wholeWordUppercase && key.charAt(0) != "1" && key.charAt(0) != "*") {
  82.                 output += "# ";
  83.             }
  84.             output += key;
  85.             if(i < word.length-1 && getKey(word.charAt(i+1)).charAt(0) == key.charAt(0)) {
  86.                 output += "_";
  87.             } else {
  88.                 output += " ";
  89.             }
  90.         }
  91.         if(word != str[str.length-1]) output += "0 ";
  92.     }
  93.     return output;
  94. }
  95.  
  96. // test
  97. console.log(to_t9('Hello World')); //# 44 33 555_555 666 0 # 9 666 777 555 3
  98. console.log(to_t9('HELLO WORLD')); //## 44 33 555_555 666 0 9 666 777 555 3
  99. console.log(to_t9('abba feed high')); //2_22_22_2 0 333_33_33_3 0 44_444_4_44
  100. console.log(to_t9('I love PHP')); //# 444 0 555 666 888 33 0 ## 7 44 7
  101. console.log(to_t9('there r 4 lights!')); //8 44 33 777 33 0 777 0 4444 0 555 444_4_44 8 7777 1111
Add Comment
Please, Sign In to add comment