Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.6 Length bug #14

Open
dawsbot opened this issue May 21, 2017 · 5 comments
Open

1.6 Length bug #14

dawsbot opened this issue May 21, 2017 · 5 comments

Comments

@dawsbot
Copy link
Contributor

dawsbot commented May 21, 2017

The way that string length is compared and calculated is not correct when strings have significant entropy. See my branch showing this here @tankwan

@dawsbot dawsbot changed the title 1.6 length bug 1.6 Length bug May 21, 2017
@dawsbot
Copy link
Contributor Author

dawsbot commented May 21, 2017

Do you want to fix this @tankwan? I can if not.

@heytonytan
Copy link
Collaborator

heytonytan commented May 22, 2017 via email

@profnandaa
Copy link
Collaborator

@thetonytan - ping!

@yuriaps
Copy link

yuriaps commented Sep 27, 2020

Bug in this test
console.log('aabbcc', strComp('aabbcc'), 'aabbcc')
As the book says, if the length of the result is not less than the original should print the original, and it is printing:
a2b2c2

@yuriaps
Copy link

yuriaps commented Sep 27, 2020

I don't see why use the Math.max()...
I would go this way

var strComp = function(string) {
    var compressed = '';
    var currChar = '';
    var currCount = '';
    for (var i = 0; i < string.length; i++) {
      if (currChar !== string[i]) {
        compressed = compressed + currChar + currCount;
        currChar = string[i];
        currCount = 1;
      } else {
        currCount++;
      }
    }
    compressed = compressed + currChar + currCount;
  
    return compressed.length >= string.length ? string : compressed;
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants