I want to know that how many times each alphabet comes in the 'input' variable. For this I am loop through each character and storing them in an object and also how many they appeared in the sentence. But It is consoling NaN. Please show me where is the error?
var input = "why this kolaveri kolaveri di";
function processData(input) {
var object = {};
input.replace(/\s/g,"").split("").forEach(function(item){
object[item] == 'undefined' ? object[item] = 0 && object[item]++ : object[item]++ ;
});
console.log(object);
}
object[item] == 'undefined'needs to beobject[item] == undefinedwith no quotes aroundundefinedand it would be even better to useobject[item] === undefinedwith the===so there's no type conversion going on. Though, I personally would useobject.hasOwnProperty(item)myself.