The problem
The primary concept is to depend all of the occurring characters in a string. You probably have a string like aba
, then the outcome ought to be {'a': 2, 'b': 1}
.
What if the string is empty? Then the outcome ought to be an empty object literal, {}
.
The answer in Javascript
Possibility 1:
operate depend (string) {
var depend = {};
string.cut up('').forEach(operate(s) {
depend[s] ? depend[s]++ : depend[s] = 1;
});
return depend;
}
Possibility 2:
operate depend (string) {
return string.cut up('').cut back(operate(counts,char),{});
}
Possibility 3:
const depend = string =>
[...string].cut back((pre, val) => (pre[val] = -~pre[val], pre), {});
Check instances to validate our resolution
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold=0;
describe("Depend characters", operate(){
operate 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 aren't strictly equal, they each have to be Objects
if ( x.constructor !== y.constructor ) return false;
// they should have the very same prototype chain, the closest we are able to 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 match 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, Capabilities, 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", operate(){
//Check.assertEquals(true, objectEqual(depend(""), {}));
assert.deepEqual(depend(""), {});
});
it ("ought to get consequently two A characters", operate () {
//Check.assertEquals(true, objectEqual(depend("aa"), { a: 2 }));
assert.deepEqual(depend("aa"), { a: 2 });
});
it ("ought to get because of two a characters and two b characters", operate () {
//Check.assertEquals(true, objectEqual(depend("aabb"), { a: 2, b: 2 }));
assert.deepEqual(depend("aabb"), { a: 2, b: 2 });
});
it ("ought to get because of two a characters and two b characters, displaying that the outcome order doesn't matter", operate () {
//Check.assertEquals(true, objectEqual(depend("aabb"), { b: 2, a: 2 }));
assert.deepEqual(depend("aabb"), { b: 2, a: 2 });
});
it("ought to examine for including various variety of characters, random variety of a's and b's", operate () {
let phrase = "", a = 0, b = 0;
for (; a < Math.random() * 100; a++) {
phrase += "a";
}
for (; b < Math.random() * 100; b++) {
phrase += "b";
}
//Check.assertEquals(true, objectEqual(depend(phrase), { "a": a, "b": b }));
assert.deepEqual(depend(phrase), { "a": a, "b": b });
});
});