Skip to content
Open
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
222 changes: 222 additions & 0 deletions Words/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Words/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "words",
"version": "1.0.0",
"description": "words function",
"main": "./src/main.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "mocha ./tests/testwords.js"
},
"keywords": [
"function"
],
"author": "mmaureen",
"license": "ISC",
"dependencies": {
"babel": "^6.23.0",
"chai": "^4.1.2",
"mocha": "^4.0.1"
}
}
32 changes: 32 additions & 0 deletions Words/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports={

countWords:(str)=>{
// check if input is empty string
if(str===""){
return "enter a string"

}
//check if input is not a string
if(typeof str !== "string"){
return "enter a string"
}


//string divides to words
let words= str.split(" ");

let finalResult ={};

for(let value of words){
// let reg = new RegExp(value,'gi');
// let output=str.match(reg);
finalResult[value.toLowerCase()] = finalResult[value.toLowerCase()] + 1 || 1;
}

return finalResult;

}

}


31 changes: 31 additions & 0 deletions Words/tests/testwords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const expect = require('chai').expect;
const Wrds =require('../src/main.js');

describe ("words",()=>{
describe ("handles correct input" ,()=>{
it("should return {my:2,name:1,is:1,joy:1}",()=>{
expect(Wrds.countWords("my my name is joy")).to.have.property('my', 2);
})
it("should return {mary:3,is:1,a:1,girl:1}",()=>{
expect(Wrds.countWords("my my name is joy")).to.have.property('joy', 1);
})


})

describe ("handles incorrect input",()=>{
it("should return not string as words for empty string",()=>{
expect(Wrds.countWords("")).to.eql("enter a string");
})

it("should return not string as words for 6",()=>{
expect(Wrds.countWords(6)).to.eql("enter a string");
})

})
})