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
71 changes: 69 additions & 2 deletions ex7/ex7.js
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"
Expand All @@ -9,3 +9,70 @@
* mostFrequentChar("a b c d e f") === " "
* mostFrequentChar("") === null
*/

function mostFrequentChar(str) {

objCharCount = {};
Copy link
Owner

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 :(


if (str === '') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this if first, before the objCharCount = {};

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not clear. I would instead add another variable called moreThanOneMax and set it to true here (then check for it below).

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing var.


if (objCharCount[currentChar] === undefined) {

objCharCount[currentChar] = 1;

} else {

objCharCount[currentChar] = objCharCount[currentChar] += 1;

}
}
return objCharCount;
}

module.exports = mostFrequentChar;
24 changes: 24 additions & 0 deletions ex7/ex7Spec.js
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();
});
});