0

Problem:

Call two arms equally strong if the heaviest weights they each are able to lift are equal.

Call two people equally strong if their strongest arms are equally strong (the strongest arm can be both the right and the left), and so are their weakest arms.

Given your and your friend's arms' lifting capabilities find out if you two are equally strong.

Issue:

My solution keeps showing false when all numbers are equal. For example, when >you = [10, 15] and friend = [10, 15], I'm getting false when it should be true. Sure it's user error over here. :)

function areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight) {
    var you = [yourLeft, yourRight];
    var friend = [friendsLeft, friendsRight];
    you.sort(), friend.sort();
    console.log(you, friend)
    if(you === friend){
        return true;
    }else{
        return false;
    }
}

Fixed, thank you!!

function areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight) {
    var you = [yourLeft, yourRight];
    var friend = [friendsLeft, friendsRight];
    you.sort(), friend.sort();
    console.log(you, friend)
    for(var i = 0; i < you.length; i++) {
        if(you[i] !== friend[i])
            return false;
    }

    return true;
}
Community
  • 1
  • 1
Almost_Ashleigh
  • 524
  • 1
  • 4
  • 24
  • 1
    using a `===` on arrays will compare references: not values... see [How to compare arrays in javascript](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Patrick Barr Jun 08 '17 at 19:02
  • you can not do === against a array it wont match you need to loop through them il get a answer down now for you – Josh Stevens Jun 08 '17 at 19:05
  • Assuming your values are numbers, [your sorting method doesn't work either](https://stackoverflow.com/q/1063007/1048572) – Bergi Jun 08 '17 at 19:15
  • @Bergi Thanks for your help. I was reading about the function for sorting numbers on MDN but realized the sort was working so they must be strings. Thanks! – Almost_Ashleigh Jun 08 '17 at 19:17
  • @Bergi Also, I apologize if this is an exact duplicate of the other page - I don't understand JSON yet so I thought it was entirely different. – Almost_Ashleigh Jun 08 '17 at 19:20

3 Answers3

1

Your issue is you are trying to compare a list of items against a another list of items without even reading the items in the list. When you do a === you are comparing the value of that item and this can not be a array as its not clever enough to loop through them all and compare (this is why for each exists ;) - so "10" === "10 = true but ["10"] === ["10"] = false

You have a array so you need to loop through and make sure every index of the element match.. i am assuming you will always have equal number in both arrays so you can do something like this:

function areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight) {
    var you = [yourLeft, yourRight];
    var friend = [friendsLeft, friendsRight];
    you.sort(), friend.sort();
    console.log(you, friend)
    for(var i = 0; i < you.length; i++) {
        if(you[i] !== friend[i])
            return false;
    }

    return true;
}
Josh Stevens
  • 3,943
  • 1
  • 15
  • 22
0

You get 'false' result because arrays in JavaScript are compared by references but not by values. In order to find out if two arrays contain elements with the same values you should use the following approach.

function areArraysEqual(array1, array2) {
  var result = true;
  var length1 = array1.length;
  var length2 = array2.length;
  if (length1 !== length2) {
    return false;
  }
  array1.sort();
  array2.sort();
  for (var i = 0; i < length1; i++) {
    if (array1[i] !== array2[i]) {
      result = false;
      break;
    }
  }
  return result;
}
0

One approach is converting the arrays to JSON objects. This one, works:

function areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight) {
var you = [yourLeft,yourRight];
var friend = [friendsLeft,friendsRight];
you.sort(), friend.sort();
you = JSON.stringify(you);
friend = JSON.stringify(friend);
console.log(you, friend)
if(you == friend){
    return true;
}else{
    return false;
}
}
treecon
  • 2,415
  • 2
  • 14
  • 28