Skip to content

count character in a string #8

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

Merged
merged 1 commit into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Algorithms in Javascript",
"main": "index.js",
"scripts": {
"test": "nyc --reporter=lcov --reporter=text mocha --exit"
"test": "nyc --reporter=lcov --reporter=text mocha **/*.test.js --exit"
},
"repository": {
"type": "git",
Expand All @@ -27,5 +27,8 @@
"chai": "^4.2.0",
"mocha": "^7.0.1",
"nyc": "^15.0.0"
},
"nyc": {
"exclude": "**/*.test.js"
}
}
Empty file.
8 changes: 8 additions & 0 deletions reduce/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { assert, expect } = require("chai");
const totalAgeValue = require("./reduce");

describe("string return char counts", () => {
it("should return true for testing", () => {
expect(true).to.equal(true);
});
});
6 changes: 4 additions & 2 deletions reduce/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ const peopleInCommunity = [
* Read more on:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
*/
/**
* write this as a function before writing testing
*/
const totalAgeValue = peopleInCommunity.reduce(
// Destructure age and noOfPeople from peopleInCommunity array
(accumulator, { age, noOfPeople }) =>
// Multiply the age and noOfPeople and add to accumulator
accumulator + (age * noOfPeople)
, 0)

// Log value of total age manipulations
console.log('Average age: ', totalAgeValue)
exports = module.exports = totalAgeValue;
14 changes: 14 additions & 0 deletions stringreturncountsofchar/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { expect, assert } = require("chai");
const charCounts = require("./solution");
const charCountsForOf = require("./solutionforof");

describe("string return char counts", () => {
it("should return counts", () => {
const str = "string to Count. Here we are + me S3456.!"
const counts = charCounts(str);
const countsForOf = charCountsForOf(str);
// Need to add more test cases ->
expect(counts.e).to.equal(5);
assert(counts.c, countsForOf.c);
});
});
4 changes: 4 additions & 0 deletions stringreturncountsofchar/question.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This part contains the question

-> write a function which takes a string and returns
counts of each character in the string
24 changes: 24 additions & 0 deletions stringreturncountsofchar/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* The problem to this question is found inside
* question.txt
*/
const charCounts = (str) => {
let result = {};
for (let i = 0; i < str.length; i++) {
// have regex to check for only alphanumeric
const regexAlphaNumeric = /^[a-z0-9]+$/i;
// assign character and make sure we lower case them
const character = str[i].toLowerCase();
if (regexAlphaNumeric.test(character)) {
if (result[character] > 0) {
// if result is greater than 0, we should add one
result[character]++;
} else {
result[character] = 1;
}
}
}
return result;
};

exports = module.exports = charCounts;
24 changes: 24 additions & 0 deletions stringreturncountsofchar/solutionforof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* The problem to this question is found inside
* question.txt
*/
const charCountsForOf = (str) => {
let result = {};
for (let character of str) {
// have regex to check for only alphanumeric
const regexAlphaNumeric = /^[a-z0-9]+$/i;
// assign character and make sure we lower case them
character = character.toLowerCase();
if (regexAlphaNumeric.test(character)) {
if (result[character] > 0) {
// if result is greater than 0, we should add one
result[character]++;
} else {
result[character] = 1;
}
}
}
return result;
};

exports = module.exports = charCountsForOf;
2 changes: 1 addition & 1 deletion test/sumnumbersupton.js → sumnumbersupton/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { assert, expect } = require("chai");
const { firstApproach, secondApproach } = require("../sumnumbersupton");
const { firstApproach, secondApproach } = require(".");

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