I created a module which analyzes a string. It counts e.g. the occurences of the letter o in a string:
export default (function () {
var analysis = {
occurencesOfO: 0,
};
var util = {
countOs: function(input) {
for (let i = 0; i < input.length; i++) {
analysis.occurencesOfO += input[i] === 'o' ? 1 : 0;
}
},
}
return {
analyze: function (input) {
util.countOs(input);
return analysis.occurencesOfO;
}
};
})();
I use it like this:
import Analysis from './Analysis';
//...
let s = 'foobar';
let result = Analysis.analyze(s);
console.log('should yield: 2 but yields: ' + result);
// should yield: 2 but yields: 2
s = 'foo bar';
result = Analysis.analyze('fooo bar');
console.log('should yield: 3 but yields: ' + result);
// should yield: 3 but yields: 5
s = 'foo bar foobar';
result = Analysis.analyze('foo bar foobar');
console.log('should yield: 4 but yields: ' + result);
// should yield: 4 but yields: 9
I would have expected that analysis gets reset, but it doesn't. Why? And how do I change that?