Advertisement
nikolayneykov

Untitled

May 19th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve ([str]) {
  2.   let words = {}
  3.   for (let i = 0; i < str.length; i++) {
  4.     let word = []
  5.  
  6.     let code = str[i].charCodeAt(0)
  7.  
  8.     while (
  9.       i < str.length &&
  10.       ((code >= 65 && code <= 90) ||
  11.         (code >= 97 && code <= 122) ||
  12.         (code >= 48 && code <= 57) ||
  13.         code === 95)
  14.     ) {
  15.       word.push(String.fromCharCode(code))
  16.       i++
  17.       if (i < str.length) {
  18.         code = str[i].charCodeAt(0)
  19.       }
  20.     }
  21.     word = word.join('')
  22.  
  23.     if (word.length > 0) {
  24.       if (!words.hasOwnProperty(word)) {
  25.         words[word] = 0
  26.       }
  27.  
  28.       words[word]++
  29.     }
  30.   }
  31.  
  32.   console.log(JSON.stringify(words))
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement