Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!