Home > Blockchain >  How to check how many times character from string appears?
How to check how many times character from string appears?

Time:06-19

I want to count how many times each letter from params appears in this string. What am I doing wrong?

function solution(word) {
    let temp = {}
    for(const c of word) {
      temp[c]  ;
      console.log(temp[c])
  }
  
}

solution("PAPAYA")

It should output me numbers below for each letter, but i keep getting NaN

1 // P appeared once
1 // A appeared once
2 // P appeared second time
2 // A appeaed second time
1 // Y Once
3 // A appeared third time

so it should look like

{
  A: 3,
  P: 2,
  Y: 1
}

CodePudding user response:

Here is an easy solution without changing your code to much

function solution(word) {
    let temp = {}
    for(const c of word) {
      if (temp[c] === undefined)
        temp[c] = 1;
       else temp[c]  ;
  }
  console.log(temp)
}

solution("PAPAYA")

CodePudding user response:

At the start, no properties are set on the object, so accessing the value for a character results in undefined. Incrementing that produces NaN. You will need to specifically handle that case for the first time a letter appears.

function solution(word) {
    let temp = {}
    for(const c of word) 
      temp[c] = (temp[c] || 0)   1;
    return temp;
}
console.log(solution("PAPAYA"));

CodePudding user response:

set temp[c] to a number (0) first for example

if (NaN(temp[c])) temp[c] = 0

The reason for this is because if you do on undefined, it is still undefined, and therefore, not a number

CodePudding user response:

const solution = (str) => {
  const result = {}
  
  str.split('').forEach(letter => {
    result[letter] = result[letter] ? result[letter]   1 : 1
  })
  
  return result
}

console.log(solution("PAPAYA"))

CodePudding user response:

Simplest solution

function solution(word) {
    let temp = {}
    for(const c of word) {
      if(c in temp)
         temp[c]  ;
      else temp[c] = 1;
  }
  
}

solution("PAPAYA")

CodePudding user response:

Solution

let counter = str => {
  return str.split('').reduce((total, letter) => {
    total[letter] ? total[letter]   : total[letter] = 1;
    return total;
  }, {});
};
console.log(counter("PAPAYA"));

//count
let unordered = counter("PAPAYA");

//sort the count object
const ordered = Object.keys(unordered).sort().reduce(
  (obj, key) => { 
    obj[key] = unordered[key]; 
    return obj;
  }, 
  {}
);

console.log(ordered);

CodePudding user response:

Easy solution using short-circuit in javascript:

function solution(word) {
    let temp = {}
    for(const c of word) {
      temp[c] = temp[c] 1 || 1;
  }

  console.log(temp);
}

solution("PAPAYA")

The issue with your solution is that the first time the value is undefined so you were increasing undefined by 1, that's why you were getting NaN (Not A Number).

short-circuit will solve that, if it is not defined, start counting from one

CodePudding user response:

function solution(word) {
  let charMap = {};
  word.split("").forEach((eachChar) => {
    if (!charMap[eachChar]) {
      charMap[eachChar] = 1;
    } else {
      charMap[eachChar]  = 1;
    }
  });
  console.log(charMap);
  return charMap;
}

solution("ppiiyaaallll");

  • Related