Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know it's simple answer but can't understand what that freq[caracter] is? And how it can be incremented if it's a string. Please be detailed

What I have tried:

function getFrequency(string) {
var freq = {};
for (var i=0; i<string.length;i++) {
var character = string.charAt(i);
if (freq[character]) {
freq[character]++;
} else {
freq[character] = 1;
}
}

return freq;
};
Posted
Updated 10-Jul-17 8:58am

Quote:
And how it can be incremented if it's a string.

The fact it is not a string may help. freq() is an array.
Use the debugger, debugging is not its main purpose, the debugger allow to execute the code step by step and to inspect variables between each step. it will show you that freq() is an array that count how many times each char appeazr in the string.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Richard Deeming 10-Jul-17 14:48pm    
"freq() is an array."

No, it's not. It's an object. :)
Patrice T 10-Jul-17 16:44pm    
Used like an associative array.
freq is an object: Working with objects - JavaScript | MDN[^]

Objects have properties. In Javascript, there are two ways to access properties. By name:
var x = someObj.myProperty;
someObj.otherProperty = 42;

Or by index:
var x = someObj['myProperty'];
someObj['otherProperty'] = 42;

Using the index is particularly useful if you don't know the name of the property until runtime.

If the specified property has not been defined, then it will return undefined. The if condition is making use of one of the uglier areas of Javascript - "truthy" and "falsy" values:
Truthy and Falsy: When All is Not Equal in JavaScript — SitePoint[^]

This could be rewritten to make use of the hasOwnProperty method[^], which might make the code slightly clearer, albeit more verbose:
if (freq.hasOwnProperty(character)){

If the specified property doesn't exist, you initialize it to the number 1.

Which is why, on subsequent iterations of the loop when the property does exist, you are able to increment it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900