File tree 9 files changed +83
-4
lines changed
9 files changed +83
-4
lines changed Original file line number Diff line number Diff line change 4
4
"description" : " Algorithms in Javascript" ,
5
5
"main" : " index.js" ,
6
6
"scripts" : {
7
- "test" : " nyc --reporter=lcov --reporter=text mocha --exit"
7
+ "test" : " nyc --reporter=lcov --reporter=text mocha **/*.test.js --exit"
8
8
},
9
9
"repository" : {
10
10
"type" : " git" ,
27
27
"chai" : " ^4.2.0" ,
28
28
"mocha" : " ^7.0.1" ,
29
29
"nyc" : " ^15.0.0"
30
+ },
31
+ "nyc" : {
32
+ "exclude" : " **/*.test.js"
30
33
}
31
34
}
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -37,12 +37,14 @@ const peopleInCommunity = [
37
37
* Read more on:
38
38
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
39
39
*/
40
+ /**
41
+ * write this as a function before writing testing
42
+ */
40
43
const totalAgeValue = peopleInCommunity . reduce (
41
44
// Destructure age and noOfPeople from peopleInCommunity array
42
45
( accumulator , { age, noOfPeople } ) =>
43
46
// Multiply the age and noOfPeople and add to accumulator
44
47
accumulator + ( age * noOfPeople )
45
48
, 0 )
46
49
47
- // Log value of total age manipulations
48
- console . log ( 'Average age: ' , totalAgeValue )
50
+ exports = module . exports = totalAgeValue ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 - z 0 - 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 ;
Original file line number Diff line number Diff line change
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 - z 0 - 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 ;
Original file line number Diff line number Diff line change 1
1
const { assert, expect } = require ( "chai" ) ;
2
- const { firstApproach, secondApproach } = require ( "../sumnumbersupton " ) ;
2
+ const { firstApproach, secondApproach } = require ( "." ) ;
3
3
4
4
describe ( "sum of numbers upto n" , ( ) => {
5
5
it ( "should test first approach to be mathematically true" , ( ) => {
You can’t perform that action at this time.
0 commit comments