Sunday, April 14, 2024
HomeSoftware EngineeringHow you can Rely Characters in a Javascript String

How you can Rely Characters in a Javascript String


The problem

The primary thought is to rely all of the occurring characters in a string. When you have a string like aba, then the consequence must be {'a': 2, 'b': 1}.

What if the string is empty? Then the consequence must be an empty object literal, {}.

The answer in Javascript

Choice 1:

perform rely (string) {  
  var rely = {};
  string.cut up('').forEach(perform(s) {
     rely[s] ? rely[s]++ : rely[s] = 1;
  });
  return rely;
}

Choice 2:

perform rely (string) {
  return string.cut up('').cut back(perform(counts,char),{});
}

Choice 3:

const rely = string =>
  [...string].cut back((pre, val) => (pre[val] = -~pre[val], pre), {});

Take a look at circumstances to validate our resolution

const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold=0;

describe("Rely characters", perform(){
    perform objectEqual( x, y ) {
      if ( x === y ) return true;
        // if each x and y are null or undefined and precisely the identical
    
      if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) return false;
        // if they don't seem to be strictly equal, they each should be Objects
    
      if ( x.constructor !== y.constructor ) return false;
        // they should have the very same prototype chain, the closest we will do is
        // take a look at there constructor.
    
      for ( let p in x ) {
        if ( ! x.hasOwnProperty( p ) ) proceed;
          // different properties have been examined utilizing x.constructor === y.constructor
    
        if ( ! y.hasOwnProperty( p ) ) return false;
          // permits to check x[ p ] and y[ p ] when set to undefined
    
        if ( x[ p ] === y[ p ] ) proceed;
          // if they've the identical strict worth or id then they're equal
    
        if ( typeof( x[ p ] ) !== "object" ) return false;
          // Numbers, Strings, Features, Booleans have to be strictly equal
    
        if ( ! Object.equals( x[ p ],  y[ p ] ) ) return false;
          // Objects and Arrays have to be examined recursively
      }
    
      for ( p in y ) {
        if ( y.hasOwnProperty( p ) && ! x.hasOwnProperty( p ) ) return false;
          // permits x[ p ] to be set to undefined
      }
      return true;
    }
    
    it ("ought to give empty object literal if string is empty", perform(){
      //Take a look at.assertEquals(true, objectEqual(rely(""), {}));
      assert.deepEqual(rely(""), {});
    });
    
    it ("ought to get because of this two A characters", perform () {
      //Take a look at.assertEquals(true, objectEqual(rely("aa"), { a: 2 })); 
      assert.deepEqual(rely("aa"), { a: 2 }); 
    });
    
    it ("ought to get because of two a characters and two b characters", perform () {
      //Take a look at.assertEquals(true, objectEqual(rely("aabb"), { a: 2, b: 2 })); 
      assert.deepEqual(rely("aabb"), { a: 2, b: 2 }); 
    });
    
    it ("ought to get because of two a characters and two b characters, displaying that the consequence order doesn't matter", perform () {
      //Take a look at.assertEquals(true, objectEqual(rely("aabb"), { b: 2, a: 2 })); 
      assert.deepEqual(rely("aabb"), { b: 2, a: 2 }); 
    });
    
    it("ought to test for including various variety of characters, random variety of a's and b's", perform () {
      
      let phrase = "", a = 0, b = 0;
      
      for (; a < Math.random() * 100; a++) {
        phrase += "a";
      }

      for (; b < Math.random() * 100; b++) {
        phrase += "b";
      }
    
      //Take a look at.assertEquals(true, objectEqual(rely(phrase), { "a": a, "b": b })); 
      assert.deepEqual(rely(phrase), { "a": a, "b": b });
    });
});
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments