-
Notifications
You must be signed in to change notification settings - Fork 1
Ex7 b #13
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
base: master
Are you sure you want to change the base?
Ex7 b #13
Changes from all commits
85316b4
01776e1
ba368e2
8dcc6e7
4a7a3d3
8fe5d54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| /** | ||
| * Write a function that returns the character that repeats itself the most in | ||
| * a given string. If there isn't one, returns null. | ||
| * mostFrequentChar returns the character that repeats itself the most in str. | ||
| * If there isn't one, returns null. | ||
| * | ||
| * For example: | ||
| * mostFrequentChar("zuriel yahav") === "a" | ||
|
|
@@ -9,3 +9,70 @@ | |
| * mostFrequentChar("a b c d e f") === " " | ||
| * mostFrequentChar("") === null | ||
| */ | ||
|
|
||
| function mostFrequentChar(str) { | ||
|
|
||
| objCharCount = {}; | ||
|
|
||
| if (str === '') { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do this if first, before the |
||
| return null; | ||
| } | ||
|
|
||
| objCharCount = countChar(str); | ||
|
|
||
| return getFrequentChar(objCharCount); | ||
| } | ||
|
|
||
| function getFrequentChar(objCharCount) { | ||
|
|
||
| var maxCount = 0, | ||
| frequentChar = null, | ||
| frequentCount = 0, | ||
| arrayChar = [], | ||
| currentCount = 0, | ||
| currentChar = ''; | ||
|
|
||
| arrayChar = Object.keys(objCharCount); | ||
|
|
||
| for (var i = 0; i < arrayChar.length; i++) { | ||
|
|
||
| currentChar = arrayChar[i]; | ||
| currentCount = objCharCount[currentChar]; | ||
|
|
||
| if (maxCount < currentCount) { | ||
|
|
||
| maxCount = currentCount; | ||
| frequentChar = currentChar; | ||
|
|
||
| } else if (maxCount === currentCount) { | ||
|
|
||
| frequentChar = null; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not clear. I would instead add another variable called Also, I think this will fail on this instance: 1122333. |
||
|
|
||
| } | ||
| } | ||
| return frequentChar; | ||
| } | ||
|
|
||
| function countChar(str) { | ||
|
|
||
| var objCharCount = {}; | ||
|
|
||
| for (var i = 0; i < str.length; i++) { | ||
|
|
||
| // The char we are now working on | ||
| currentChar = str.charAt(i); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing |
||
|
|
||
| if (objCharCount[currentChar] === undefined) { | ||
|
|
||
| objCharCount[currentChar] = 1; | ||
|
|
||
| } else { | ||
|
|
||
| objCharCount[currentChar] = objCharCount[currentChar] += 1; | ||
|
|
||
| } | ||
| } | ||
| return objCharCount; | ||
| } | ||
|
|
||
| module.exports = mostFrequentChar; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| var mostFrequentChar= require('./ex7.js'); | ||
|
|
||
| describe("mostFrequentChar", function() { | ||
|
|
||
| it("finds one char that repeats the most", function(){ | ||
| expect(mostFrequentChar('zuriel yahav')).toBe('a'); | ||
| }); | ||
|
|
||
| it("returns null because more than one char repeats twice", function(){ | ||
| expect(mostFrequentChar('ohad kravchick')).toBeNull(); | ||
| }); | ||
|
|
||
| it("finds one char that repeats the most (3)", function(){ | ||
| expect(mostFrequentChar('dana kravchick')).toBe('a'); | ||
| }); | ||
|
|
||
| it("finds a whitespace as the most frequent char", function(){ | ||
| expect(mostFrequentChar('a b c d e f')).toBe(' '); | ||
| }); | ||
|
|
||
| it("returns null because input is empty", function(){ | ||
| expect(mostFrequentChar('')).toBeNull(); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing
var-- this is now defined on the window :(