Check how often a char is used in a sentence with objects

var data = "check how often chars gets used in this sentence";
var result = {};
var i;
var len = data.length;
for (i = 0; i < len; i++) {
  var currentChar = data.charAt(i);
  if(currentChar === " "){
    currentChar = "whitespace";
  }
  
  if(result[currentChar] === undefined) {
    result[currentChar] = 1;
  } else {
    result[currentChar] += 1;
  }
}
//to just see how often one character is used you can target it like this:
console.log(result['c']);// -> 4

//to check all occurences
console.log(result); //-> 
/*a: 1,
  c: 4,
  d: 1,
  e: 7,
  f: 1,
  g: 1,
  h: 4,
  i: 2,
  k: 1,
  n: 4,
  o: 2,
  r: 1,
  s: 5,
  t: 4,
  u: 1,
  w: 1,
  whitespace: 8
*/

Add your comment