Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String.prototype.hashify = function () {
- const arr = this.toString().split('');
- let obj = {};
- let previousKey, currentKey;
- for (let i=0; i<arr.length; i++) {
- currentKey = arr[i];
- if (!obj[currentKey]) {
- if (i < arr.length -1) {
- obj[currentKey] = [arr[i+1]];
- }
- } else {
- console.log('ELSE', i);
- obj[previousKey].push(arr[i+1]);
- }
- if (i === arr.length - 1) {
- console.log('LAST', currentKey);
- obj[currentKey] = [arr[0]];
- }
- previousKey = currentKey;
- console.log(obj);
- }
- // console.log(obj);
- return obj;
- };
- console.log('raracrceaec'.hashify());
- // Correct results is {"r":["a","r"],"a":["c","r"],"c":["e","a"],"e":"c"} - Expected: 'true', instead got: 'false'
- /**
- '123456'.hashify() == {"1":"2","2":"3","3":"4","4":"5","5":"6","6":"1"} // The last char will be key for 1st
- char
- '11223'.hashify() == {"1":["1","2"],"2":["2","3"],"3":"1"} // when duplicates exist, group them in an array
- // i.e. 1 is key for next char 1, that 1 is key for next char 2, but 1 is already in the hash, so add 2 to key 1
- 'Codewars'.hashify() == {"C":"o","o":"d","d":"e","e":"w","w":"a","a":"r","r":"s","s":"C"}
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement