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

147
Vistas
Compare value of two arrays

Five random numbers(from numbers array) will be shown one by one and we need to recall last four number by clicking the number. The answers are push to answer array after clicking. Now i need to compare answers array and number array to calculate the score. My problem is in scoring logic which is not working as expected. When the first guess(or any guess) is correct, then all the remaining guess are also counted as correct even its not.

link: https://codesandbox.io/s/pensive-pascal-rh6e13?file=/src/App.js

Below is the score calculation logic

useEffect(() => {
    if (answers.length) {
      if (numbers[4].num === answers[3]) {
        console.log(
          "match is--------------------------------1",
          numbers[4].num,
          answers[3]
        );
        setScore(score=> score + 1);
      } else if (numbers[3].num === answers[2]) {
        console.log(
          "match is--------------------------------2",
          numbers[3].num,
          answers[2]
        );
        setScore(score=> score + 1);
      } else if (numbers[2].num === answers[1]) {
        console.log(
          "match is--------------------------------3",
          numbers[2].num,
          answers[1]
        );
        setScore(score=> score + 1);
      } else if (numbers[1].num === answers[0]) {
        console.log(
          "match is--------------------------------4",
          numbers[1].num,
          answers[0]
        );
        setScore(score=> score + 1);
      } else {
        console.log("no match---------------------");
      }
    }
  }, [answers, numbers]);

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can calculate score doing sth like this:

const myScore = useMemo(() => {
    let points = 0;
    const numbersToCompare = numbers.filter((_, index) => index > 0);
    for (let i in numbersToCompare) {
      if (numbersToCompare[i].num === answers[i]) {
        points = points + 1;
      }
    }
    return points;
  }, [answers, numbers]);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You might want to simplify your calculation logic with a loop like that:

useEffect(() => {
    console.log("Checking result", answers, numbers);
    if (answers.length < numbers.length - 1) {
        return;
    }
    let i = 0;
    while (i < numbers.length - 1) {
        if (numbers[i + 1].num === answers[i]) {
            console.log("match is--------------------------------1");
            setScore((score) => score + 1);
        }
        i++;
    }
}, [answers, numbers]);

Here you have a fork of your demo with the changes I describe below:

https://codesandbox.io/s/elegant-sara-zftstd?file=/src/App.js

about 4 years ago · Juan Pablo Isaza Denunciar

0

The comparison of two arrays can be stored in a result array using Array.map , subsequently you can use Array.reduce to calculate a score.

Something like

const arr = [...Array(5)]
  .map( _ => Math.floor(Math.random() * 10) + 1 );
const fakeAnswer = [...Array(5)]
  .map( _ => Math.floor(Math.random() * 10) + 1 );
const results = arr.map((v, i) => v === fakeAnswer[i]);
const score = results.reduce( (acc, val) => acc + +val, 0);
console.log(`random values [${arr}]`);
console.log(`fakeAnswer: [${fakeAnswer}]\nresults: [${
  results}]\nscore: ${score}`);

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