1

Task:

Give you an obj, it can be 3 types: string, number and number array, Check that they are symmetrical or not, return a Boolean value.

Example:

obj="" return true (Empty string should return true)

obj="1"   return true   (one char should return true)

obj="11"  return true

obj="12"  return false

obj="121" return true

obj=1     return true   (number<10  should return true)

obj=-1    return false  (negative number should return false)

obj=121   return true

**obj=[]    return true  (Empty array should return true)**

**obj=[1]   return true  (an array with one element should return true)**

obj=[1,2,3,4,5]      return false  

**obj=[1,2,3,2,1]      return true**

**obj=[11,12,13,12,11] return true   (left element = right element)**

obj=[11,12,21,11]    return false  (not verify them as a string)

My code fails the bolded ones and I've tried to work out why for ages!

Here's my code:

function sc(obj){
  var obj2= obj;
  if (obj==="") {
    return true;
  } else if (typeof obj === "string") {
    var revd = obj.split("").reverse("").join("");
    return revd === obj;
  } else if (typeof obj === "number") {
    var revd = parseInt(obj.toString().split("").reverse("").join(""));
    return revd === obj;
  } else if (typeof obj === "object") {
    var obj2 = []
    for (var i = obj.length-1; i >= 0; i--) {
      obj2.push(obj[i])
    }
    return obj==obj2;
  }
}
    
console.log(sc([11,12,13,12,11]));

Could anyone give me a hint as to what's going wrong?

Community
  • 1
  • 1
RMansfield
  • 31
  • 2
  • 3
    Objects are compared by reference. Two objects are never equal to each other. Have a look at [How to compare arrays in JavaScript?](http://stackoverflow.com/q/7837456/218196) – Felix Kling Dec 12 '16 at 22:53
  • You are not using an object, you're using an array. The function is designed for objects, and you're using it for an array. You should incorporate a `for` loop at the beginning, somehow. – GreenHawk1220 Dec 12 '16 at 23:09

3 Answers3

1

instead of return obj==obj2;

write the following:

for (var i=0; i<obj.length, i++) {
   if (obj[i] != obj2[i]) { return false; }
}
return true;
ryan
  • 974
  • 2
  • 7
  • 13
0

Just iterate forward and backward at the same time and check.

Here is a alrothim written in c, easy to convert to C++. Checking if an array in C is symmetric (Javascript makes life easy re-write to JavaScript and use !=)

Community
  • 1
  • 1
Mikes3ds
  • 592
  • 5
  • 12
0

You cannot compare arrays like that since they are references. You can however, create a function for comparing them like this:

function arraysEqual(arr1, arr2) {
    if(arr1.length !== arr2.length)
        return false;
    for(var i = arr1.length; i--;) {
        if(arr1[i] !== arr2[i])
            return false;
    }

    return true;
}

And then use it like this:

function arraysEqual(arr1, arr2) {
    if(arr1.length !== arr2.length)
        return false;
    for(var i = arr1.length; i--;) {
        if(arr1[i] !== arr2[i])
            return false;
    }

    return true;
}


function sc(obj){
  var obj2= obj;
  if (obj==="") {
    return true;
  } else if (typeof obj === "string") {
    var revd = obj.split("").reverse("").join("");
    return revd === obj;
  } else if (typeof obj === "number") {
    var revd = parseInt(obj.toString().split("").reverse("").join(""));
    return revd === obj;
  } else if (typeof obj === "object") {
    var obj2 = []
    for (var i = obj.length-1; i >= 0; i--) {
      obj2.push(obj[i])
    }
    return arraysEqual(obj, obj2);
  }
}

console.log(sc([11,12,13,12,11]));
Yaser
  • 5,609
  • 1
  • 15
  • 27