1

I have array listA and listB, compare and calculate the output of each list using javascript

listA=[1, 3, 5]
listB=[3, 3, 2] 

if listA[0] > listB[0] then x gets the point 1
if listA[1]==listB[1] then x and y no points
if listA[2] > listB[2] then y gets the point 1

function getPoints(x, y){
  var pointX, pointY =0
  var result = x.forEach(e=>{
   e[0] > y[0] ?  pointX++ : e[1] < y[1] pointY ++
 })
}

console.log(this.getPoints(listA, listB));
Expected Output

1 1

How to do this javascript, got stuck ,

3
  • forEach provides a second argument as the index .forEach((e, i) => ...). You can use that to access the elements of the arrays. The this keyword in the console.log is not necessary. Commented Dec 16, 2020 at 3:19
  • 1
    Several issues here. 1) You really haven't provided a proper explanation of what "compare and calculate" exactly means in this scenario. 2) Array#forEach() has no return 3) Your function has no return. Broken code is often not a good substitute for a proper explanation of exactly what you expect the code to do Commented Dec 16, 2020 at 3:19
  • 1
    What do you mean "compare and calculate the output"? What are you comparing? And what are you calculating? Commented Dec 16, 2020 at 3:27

6 Answers 6

1

Using reduce will simplify. Build [accumalate points X, accumalate points Y]

function getPoints(x, y) {
  return x.reduce(
    ([a, b], curr, i) => [curr > y[i] ? a + 1 : a, y[i] > curr ? b + 1 : b],
    [0, 0]
  );
}

listA = [1, 3, 5];
listB = [3, 3, 2];

console.log(getPoints(listA, listB));

Sign up to request clarification or add additional context in comments.

Comments

0

I see what you want to get.

But I would suggest you to change little bit

so the script should go this way

 <script>
  const listA = [1, 3, 5];
  const listB = [3, 3, 2];

  // if listA[0] > listB[0] then x gets the point 1
  // if listA[1]==listB[1] then x and y no points
  // if listA[2] > listB[2] then y gets the point 1

  const point = { a: 0, b: 0 };
  function getPoints(x, y) {
    x.forEach((item, index) => {
      if (item > y[index]) {
        point.a++;
      }
      if (item < y[index]) {
        point.b++;
      }
    });
  }
  this.getPoints(listA, listB);
  console.log(point);
</script>

I took out pointA, pointB from the function and made an object that contains a and b as properties. they will contain the total point of each parameter you pass through

hope this will solve your problem

Comments

0

To compare arrays, use follow as code:

// Warn if overriding existing method
if(Array.prototype.equals)
    console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
    // if the other array is a falsy value, return
    if (!array)
        return false;

    // compare lengths - can save a lot of time 
    if (this.length != array.length)
        return false;

    for (var i = 0, l=this.length; i < l; i++) {
        // Check if we have nested arrays
        if (this[i] instanceof Array && array[i] instanceof Array) {
            // recurse into the nested arrays
            if (!this[i].equals(array[i]))
                return false;       
        }           
        else if (this[i] != array[i]) { 
            // Warning - two different object instances will never be equal: {x:20} != {x:20}
            return false;   
        }           
    }       
    return true;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});

Example usage:

[1, 2, [3, 4]].equals([1, 2, [3, 2]]) === false;
[1, 2,3].equals([1, 2, 3]) === true;
[1, 2, [3, 4]].equals([1, 2, [3, 4]]) === true;

Comments

0

I would recommend using reduce like this. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

const listA = [2,34444,3];
const listB = [4444,33,2];


function getWinningPointsTally(listA, listB) {
  return listA.reduce((totalPoints, currentListAItem, index) => {
    if (currentListAItem > listB[index]) {
        totalPoints[0]++;
    } else if (listB[index] > currentListAItem) {
        totalPoints[1]++;
    } 
    
    return totalPoints;
  }, [0, 0]);
}

console.log(this.getWinningPointsTally(listA, listB))

Comments

0

const calcPoints = (listA, listB) => listA.reduce((acc, p, i) => [acc[0] + (p > listB[i]), acc[1] + (p < listB[i])], [0, 0])

Comments

0

A lot of ways to do this, but this should be simple and easy to understand. I chose a regular for loop since you are accessing the same index in each array.

listA = [1, 3, 5]
listB = [3, 3, 2]

function getPoints(x, y) {
  var xPoints = 0, yPoints = 0;

  for (var i = 0; i < x.length; i++)
    if (x[i] > y[i]) xPoints++
    else if (x[i] < y[i]) yPoints++;

  return [xPoints, yPoints];
}

console.log(getPoints(listA, listB)); // output: [1,1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.