0

This is what I was asked to do: Given an array of items, check each pair of items for equality and equivalence, logging out the results For example,

var arItems = [2,2,"2",true,'6'];

would output the following: 2 and 2 are equivalent 2 and 2 are equal but not equivalent 2 and true are neither equal nor equivalent true and false and neither equal nor equivalent

You cannot use the === operator, so you must use if/else and loops to check each pair

Your array should contain at least 5 items, demonstrating each valid output

Your code must be easily modifiable to change the items in your array

This is what I have so far:

var arItems = [2, 2, "2", true, '6'];
for ( x = 0; x < 5; x++) 
{
for ( y = 1; y < 5; y++)
{
    if (typeof arItems [x]== typeof arItems [y] && arItems [x] == arItems [y])
     {
         console.log (arItems [x] + " and " + arItems [y] + " are equivalent");
      }
    else if (arItems [x] == arItems [y] && typeof arItems [x] != arItems [y]) 
        {
            console.log (arItems [x] + " and " + arItems [y] + " are equal but not equivalent");
        }
    else
      {
          console.log ( arItems [x] + " and " + arItems [y] + " are neither equal nor equivalent");
      }
}
}

I am trying to get it to check the following pairs: 2 & 2 . 2 & 2 . 2 & "2" . "2" & true . true & '6' I'm not sure if there is something i can add to the code that allows it to run automatically, or if i have to write out the whole code in order for it to follow those pairs exactly as they are. It is to say it is comparing the indexes as they go along example: index [0,1,2,3,4] pairs being compared: 0 & 1 . 1 & 2 . 2 & 3 . 3 & 4

3
  • 1
    I find "you cannot use the === operator" to be a mildly disturbing instruction. Many people in the JavaScript community say to always use the === operator in place of ==. Commented Feb 13, 2017 at 4:06
  • he is trying to make us think about using other alternatives Commented Feb 13, 2017 at 4:08
  • Using other alternatives is fine for education sake, but in actual code using == is a bad practice (or at least you should leave a comment when you actually mean to use == instead of ===) Commented Feb 13, 2017 at 4:11

1 Answer 1

2

You can iterate over the items in this fashion with a single for loop that starts at index 1 and goes the the end. You can reference the previous element for each pair by subtracting 1 from the current index.

I've also updated your conditional code just to show how it would more likely be done in practice.

var items = [2, 2, "2", true, '6'];
for (var i = 1; i < items.length; i++) {
  var item1 = items[i - 1];
  var item2 = items[i];
  if (item1 === item2) {
    console.log(item1, "and", item2, "are equivalent");
  } else if (item1 == item2) {
    console.log(item1, "and", item2, "are equal but not equivalent");
  } else {
    console.log(item1, "and", item2, "are neither equal nor equivalent");
  }
}

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

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.