jsancho
jsancho

Reputation: 55

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]);

Upvotes: 0

Views: 100

Answers (3)

KooiInc
KooiInc

Reputation: 123016

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}`);

Upvotes: 1

Pablo Aragon
Pablo Aragon

Reputation: 333

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

Upvotes: 1

Bhuwan Adhikari
Bhuwan Adhikari

Reputation: 1009

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]);

Upvotes: 1

Related Questions