From ba368e279aa76d2d31c48c16b79f41aecbd588f5 Mon Sep 17 00:00:00 2001 From: Zuriel Yahav Date: Tue, 6 Dec 2016 11:11:06 +0200 Subject: [PATCH 1/4] Done --- ex7/ex7.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++-- ex7/ex7Spec.js | 25 +++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 ex7/ex7Spec.js diff --git a/ex7/ex7.js b/ex7/ex7.js index 3a115e1..d91f0b2 100644 --- a/ex7/ex7.js +++ b/ex7/ex7.js @@ -1,6 +1,7 @@ /** - * 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 tha + * t repeats itself the most in str. + * If there isn't one, returns null. * * For example: * mostFrequentChar("zuriel yahav") === "a" @@ -9,3 +10,59 @@ * mostFrequentChar("a b c d e f") === " " * mostFrequentChar("") === null */ + +function mostFrequentChar(str) { + + // If str empty tmpChar will return NULL + var winnerChar= null, index = 0, + topRepeat = { charValue: '', charCount: 0}, + search = { charValue: '', charCount: 0}, + doNotSearchList = []; + + if (str != '') { + + // Loop through all chars in str + for (var i = 0; i < str.length; i++) { + + // The char we are now working on + search.charValue = str.charAt(i); + search.charCount = 1; + + // We will count only the char + if (doNotSearchList.indexOf(search.charValue, 0) === -1) { + + // Add search char to doNotSearchList + // So we do not look for it next time + doNotSearchList.push(search.charValue); + + index = 1; + index = str.indexOf(search.charValue, i + index); + + while (index !== -1) { + + search.charCount += 1; + index = str.indexOf(search.charValue, index + 1); + } + + // Check if we have a winner, top repeat char + // Only if we do have a winner will update topRepeat + if (search.charCount > topRepeat.charCount) { + + topRepeat.charValue = search.charValue; + topRepeat.charCount = search.charCount; + winnerChar = topRepeat.charValue; + + } else if (topRepeat.charCount === search.charCount) { + // If top repeat char same to current search char count, so will set winnerChar to null + winnerChar = null; + + } + } + } + } + return winnerChar; + +} + +//mostFrequentChar("zuriel yahav"); +module.exports = mostFrequentChar; diff --git a/ex7/ex7Spec.js b/ex7/ex7Spec.js new file mode 100644 index 0000000..5364a0b --- /dev/null +++ b/ex7/ex7Spec.js @@ -0,0 +1,25 @@ +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(){ + fail(); + expect(mostFrequentChar('')).toBeNull(); + }); +}); From 8dcc6e7b883d228f80a412c94356722dc5388bd4 Mon Sep 17 00:00:00 2001 From: Zuriel Yahav Date: Tue, 6 Dec 2016 11:16:44 +0200 Subject: [PATCH 2/4] remove error --- ex7/ex7Spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/ex7/ex7Spec.js b/ex7/ex7Spec.js index 5364a0b..fbad527 100644 --- a/ex7/ex7Spec.js +++ b/ex7/ex7Spec.js @@ -19,7 +19,6 @@ describe("mostFrequentChar", function() { }); it("returns null because input is empty", function(){ - fail(); expect(mostFrequentChar('')).toBeNull(); }); }); From 4a7a3d3b4c662547afbcb25ac80502b4d1e40f14 Mon Sep 17 00:00:00 2001 From: Zuriel Yahav Date: Tue, 13 Dec 2016 17:14:40 +0200 Subject: [PATCH 3/4] Replace str != to str === --- ex7/ex7.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ex7/ex7.js b/ex7/ex7.js index d91f0b2..460f537 100644 --- a/ex7/ex7.js +++ b/ex7/ex7.js @@ -19,8 +19,9 @@ function mostFrequentChar(str) { search = { charValue: '', charCount: 0}, doNotSearchList = []; - if (str != '') { - + if (str === '') { + return null; + } // Loop through all chars in str for (var i = 0; i < str.length; i++) { @@ -59,7 +60,7 @@ function mostFrequentChar(str) { } } } - } + return winnerChar; } From 8fe5d549ee3f2ae1ce04c2307f6d22a4e37fc73b Mon Sep 17 00:00:00 2001 From: Zuriel Yahav Date: Tue, 13 Dec 2016 23:44:03 +0200 Subject: [PATCH 4/4] Count char in obj --- ex7/ex7.js | 83 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/ex7/ex7.js b/ex7/ex7.js index 460f537..5b37eac 100644 --- a/ex7/ex7.js +++ b/ex7/ex7.js @@ -1,6 +1,5 @@ /** - * mostFrequentChar returns the character tha - * t repeats itself the most in str. + * mostFrequentChar returns the character that repeats itself the most in str. * If there isn't one, returns null. * * For example: @@ -13,57 +12,67 @@ function mostFrequentChar(str) { - // If str empty tmpChar will return NULL - var winnerChar= null, index = 0, - topRepeat = { charValue: '', charCount: 0}, - search = { charValue: '', charCount: 0}, - doNotSearchList = []; + objCharCount = {}; if (str === '') { return null; } - // Loop through all chars in str - for (var i = 0; i < str.length; i++) { - // The char we are now working on - search.charValue = str.charAt(i); - search.charCount = 1; + objCharCount = countChar(str); - // We will count only the char - if (doNotSearchList.indexOf(search.charValue, 0) === -1) { + return getFrequentChar(objCharCount); +} + +function getFrequentChar(objCharCount) { + + var maxCount = 0, + frequentChar = null, + frequentCount = 0, + arrayChar = [], + currentCount = 0, + currentChar = ''; - // Add search char to doNotSearchList - // So we do not look for it next time - doNotSearchList.push(search.charValue); + arrayChar = Object.keys(objCharCount); - index = 1; - index = str.indexOf(search.charValue, i + index); + for (var i = 0; i < arrayChar.length; i++) { - while (index !== -1) { + currentChar = arrayChar[i]; + currentCount = objCharCount[currentChar]; - search.charCount += 1; - index = str.indexOf(search.charValue, index + 1); - } + if (maxCount < currentCount) { - // Check if we have a winner, top repeat char - // Only if we do have a winner will update topRepeat - if (search.charCount > topRepeat.charCount) { + maxCount = currentCount; + frequentChar = currentChar; - topRepeat.charValue = search.charValue; - topRepeat.charCount = search.charCount; - winnerChar = topRepeat.charValue; + } else if (maxCount === currentCount) { - } else if (topRepeat.charCount === search.charCount) { - // If top repeat char same to current search char count, so will set winnerChar to null - winnerChar = null; + frequentChar = null; - } - } } - - return winnerChar; + } + 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); + + if (objCharCount[currentChar] === undefined) { + + objCharCount[currentChar] = 1; + + } else { + + objCharCount[currentChar] = objCharCount[currentChar] += 1; + + } + } + return objCharCount; } -//mostFrequentChar("zuriel yahav"); module.exports = mostFrequentChar;