Skip to content

Commit b77bd8b

Browse files
authoredFeb 18, 2020
Merge pull request #8 from kemboijs/string-return-counts-of-char
count character in a string
2 parents 673c219 + 9f09860 commit b77bd8b

File tree

9 files changed

+83
-4
lines changed

9 files changed

+83
-4
lines changed
 

‎package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Algorithms in Javascript",
55
"main": "index.js",
66
"scripts": {
7-
"test": "nyc --reporter=lcov --reporter=text mocha --exit"
7+
"test": "nyc --reporter=lcov --reporter=text mocha **/*.test.js --exit"
88
},
99
"repository": {
1010
"type": "git",
@@ -27,5 +27,8 @@
2727
"chai": "^4.2.0",
2828
"mocha": "^7.0.1",
2929
"nyc": "^15.0.0"
30+
},
31+
"nyc": {
32+
"exclude": "**/*.test.js"
3033
}
3134
}

‎parsestringtoarray/index.test.js

Whitespace-only changes.

‎reduce/index.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { assert, expect } = require("chai");
2+
const totalAgeValue = require("./reduce");
3+
4+
describe("string return char counts", () => {
5+
it("should return true for testing", () => {
6+
expect(true).to.equal(true);
7+
});
8+
});

‎reduce/reduce.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ const peopleInCommunity = [
3737
* Read more on:
3838
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
3939
*/
40+
/**
41+
* write this as a function before writing testing
42+
*/
4043
const totalAgeValue = peopleInCommunity.reduce(
4144
// Destructure age and noOfPeople from peopleInCommunity array
4245
(accumulator, { age, noOfPeople }) =>
4346
// Multiply the age and noOfPeople and add to accumulator
4447
accumulator + (age * noOfPeople)
4548
, 0)
4649

47-
// Log value of total age manipulations
48-
console.log('Average age: ', totalAgeValue)
50+
exports = module.exports = totalAgeValue;
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { expect, assert } = require("chai");
2+
const charCounts = require("./solution");
3+
const charCountsForOf = require("./solutionforof");
4+
5+
describe("string return char counts", () => {
6+
it("should return counts", () => {
7+
const str = "string to Count. Here we are + me S3456.!"
8+
const counts = charCounts(str);
9+
const countsForOf = charCountsForOf(str);
10+
// Need to add more test cases ->
11+
expect(counts.e).to.equal(5);
12+
assert(counts.c, countsForOf.c);
13+
});
14+
});

‎stringreturncountsofchar/question.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This part contains the question
2+
3+
-> write a function which takes a string and returns
4+
counts of each character in the string

‎stringreturncountsofchar/solution.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* The problem to this question is found inside
3+
* question.txt
4+
*/
5+
const charCounts = (str) => {
6+
let result = {};
7+
for (let i = 0; i < str.length; i++) {
8+
// have regex to check for only alphanumeric
9+
const regexAlphaNumeric = /^[a-z0-9]+$/i;
10+
// assign character and make sure we lower case them
11+
const character = str[i].toLowerCase();
12+
if (regexAlphaNumeric.test(character)) {
13+
if (result[character] > 0) {
14+
// if result is greater than 0, we should add one
15+
result[character]++;
16+
} else {
17+
result[character] = 1;
18+
}
19+
}
20+
}
21+
return result;
22+
};
23+
24+
exports = module.exports = charCounts;
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* The problem to this question is found inside
3+
* question.txt
4+
*/
5+
const charCountsForOf = (str) => {
6+
let result = {};
7+
for (let character of str) {
8+
// have regex to check for only alphanumeric
9+
const regexAlphaNumeric = /^[a-z0-9]+$/i;
10+
// assign character and make sure we lower case them
11+
character = character.toLowerCase();
12+
if (regexAlphaNumeric.test(character)) {
13+
if (result[character] > 0) {
14+
// if result is greater than 0, we should add one
15+
result[character]++;
16+
} else {
17+
result[character] = 1;
18+
}
19+
}
20+
}
21+
return result;
22+
};
23+
24+
exports = module.exports = charCountsForOf;

‎test/sumnumbersupton.js renamed to ‎sumnumbersupton/index.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { assert, expect } = require("chai");
2-
const { firstApproach, secondApproach } = require("../sumnumbersupton");
2+
const { firstApproach, secondApproach } = require(".");
33

44
describe("sum of numbers upto n", () => {
55
it("should test first approach to be mathematically true", () => {

0 commit comments

Comments
 (0)
Please sign in to comment.