0

I did a findMaxChar count algorithm practice, manage to get it to worked but my call method can be improved.

const myStr = 'Lorem Ipsummm'

function findRepeatable(myStr){
  let charMap = {}
  for(let char of myStr){
    if(charMap[char]) {
      charMap[char]++
    }else{
      charMap[char] = 1
    }
  }
  return charMap
}

function findMostRepeated(obj){
   let maxChar = '',
   max = 0
   for(let char in obj){
     if(obj[char] > max){
       max = obj[char]
       maxChar = char
     }
  }

  return `${maxChar}: ${max}`
}

console.log(findMostRepeated(findRepeatable(myStr)))

I'm passing function as argument, how can I make it to chain like this

findMostRepeated()
.findRepeatable()

https://jsbin.com/katixefoxe/edit?html,js,console

5
  • 7
    Your solution is better then chaining because chaining requires an object with state, while your solution is functional - each function knows how to do one thing without having side effects, and you can combine multiple functions to get a result. Commented Dec 23, 2017 at 11:18
  • ^ Agreed, though if you do NEED to chain it, wrap it in a class and create instance of that class Commented Dec 23, 2017 at 11:21
  • @OriDrori passing function as argument is ok? I felt it's hard to read. Commented Dec 23, 2017 at 11:22
  • 2
    If you feel it is hard to read, then assign the first result to a variable, and pass that. That is perfectly fine. Commented Dec 23, 2017 at 11:24
  • @MadelineRies - functional programming takes some getting used to. However, when you do, it's very readable, because you can see the input, and the flow. Commented Dec 23, 2017 at 11:34

3 Answers 3

1

Many functional libraries come with a pipe function that can be used for "chaining".

pipe(findRepeatable, findMostRepeated)(myStr)

It's easy to implement yourself.

function pipe(...funcs) {
  return function(value) {
    for (func of funcs) {
      value = value(func)
    }

    return value;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

or just a single line const pipe = (...funcs) => value => funcs.reduce((v, f) => f(v), value);
0
 class CharMap extends Map {
  constructor(str){
    super();
    for(var char of str)
      this.add(char);
  }
  add(char){
    this.set(char, (this.get(char) || 0) + 1);
  }
  max(){
    return this.sortDesc()[0];
  }
  sortDesc(){
    return [...this].sort(([char, a], [char2, b]) => b - a);
  }
 }

So you can do:

 const map = new CharMap("abbccddeee");
 console.log(map.max());

To get the char and its count:

 const [char, count] = map.max();

1 Comment

this is way too advance haha!
0

try:

const myStr = 'Lorem Ipsummm'
var myObj ={
  charMap:{},
findRepeatable:function (myStr){

  for(let char of myStr){
if(this.charMap[char]) {
  this.charMap[char]++
}else{
  this.charMap[char] = 1
}
  }
  return this;
},
findMostRepeated: function (obj){
   let maxChar = '',
   max = 0
   for(let char in this.charMap){
     if(this.charMap[char] > max){
       max = this.charMap[char]
       maxChar = char
     }
  }

  return `${maxChar}: ${max}`;
}
}

console.log(myObj.findRepeatable(myStr).findMostRepeated())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.