Code Monkey home page Code Monkey logo

Comments (5)

heytonytan avatar heytonytan commented on July 22, 2024 1

from ctci-6th-edition-javascript.

profnandaa avatar profnandaa commented on July 22, 2024 1

@thetonytan - ping!

from ctci-6th-edition-javascript.

dawsbot avatar dawsbot commented on July 22, 2024

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

from ctci-6th-edition-javascript.

yuriaps avatar yuriaps commented on July 22, 2024

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

from ctci-6th-edition-javascript.

yuriaps avatar yuriaps commented on July 22, 2024

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;
  };

from ctci-6th-edition-javascript.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.