Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
Count matching letters once

I'm creating a kid's learning tool that has a form which matches 4 letters to a word.

I want to count the number of character matches in a word. But it counts duplicates of letters as 2 instead of 1. For example if the word is "loot", and the user submits "flop", the matching letters are 3 instead of 2, because it's counting "o" twice. How do I fix this? Many thanks

       function countMatching(str1, str2) {
            var c = 0;

            for (var i = 0; i < str1.length; i++) {

              if (str2.includes(str1[i]))
                c += 1;
                
            }
            matchingLetters = c;
          }
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I made an alternative version of @cmgchess' answer, which creates an array of the actual solution of letters to still guess, and removes each letter as it is encountered in the entered solution.

let matchingLetters;

function countMatching(str1, str2) {
            var c = 0;
            str1Arr = str1.split('');
            
            for (var i = 0; i < str2.length; i++) {

              if (str1Arr.includes(str2[i])) {
                c += 1;
                str1Arr.splice(str1Arr.indexOf(str2[i]), 1);
              }
                
            }
            matchingLetters = c;
          }
          
          
countMatching('loot', 'boot') 
console.log(matchingLetters)

about 4 years ago · Juan Pablo Isaza Denunciar

0

A possible approach using Sets. I converted the string into a set and back to an array so if str1 is loot then str1Set will be ['l','o','t']. The rest is the same

let matchingLetters;

function countMatching(str1, str2) {
            var c = 0;
            str1Set = [...new Set([...str1])]
            str2Set = [...new Set([...str2])]
            
            for (var i = 0; i < str1Set.length; i++) {

              if (str2Set.includes(str1Set[i]))
                c += 1;
                
            }
            matchingLetters = c;
          }
          
          
countMatching('loot', 'flop') 
console.log(matchingLetters)

about 4 years ago · Juan Pablo Isaza Denunciar

0

function countMatching(str1, str2) {
            var c = [];

            for (var i = 0; i < str1.length; i++) {

              if (str2.includes(str1[i]))
                  if (!c.includes(str1[i])) c.push(str1[i])
                
            }
            matchingLetters = c.length;
          }

I'm writing on a tab, so there might be mistakes

What I did is that I made c to be a list that contains each character shared between the two strings. And a new character is pushed into c only if the character doesn't exist in it, hence making it a list of unique characters.

Then you can get the length of the list or you can even use the list for other purposes

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda