0

I want to write a function to test if a string such as "(())" or "<<>>" is equal on both sides if they were reversed. where as something like "((()" would be false

Is there a name for something like this? I assume is a common algorithm.

For some reason, reverse doesn't do anything on right after splitting it into an array?

function ifEqual(str) {
  let left = str.slice(0, str.length / 2);
  let right = str.slice(str.length / 2).split("").reverse().join("");
  console.log(left);
  console.log(right);

  return left === right;
}

ifEqual("(())")
totalnoob
  • 2,521
  • 8
  • 35
  • 69
  • palindrome checks if a word itself is the same reversed, no? i'm trying to check if two parts of the string is the same reversed – totalnoob Aug 13 '18 at 21:00
  • 2
    @BrettBeatty "(())" is not a palindrome, nor is "<<>>" – Tomasz Bubała Aug 13 '18 at 21:01
  • More information is needed: is `<><<>>` okay? is `<><>` okay? Will only characters that have "flips" be used? – Jared Goguen Aug 13 '18 at 21:01
  • 1
    Make an object that relates each of these characters to its reversed partner, e.g. `{"<": ">", "(": ")", ...}`. Then you can loop over the characters in the string and check whether they match their partner. – Barmar Aug 13 '18 at 21:02
  • think about it, reverse is NOT a mirror flip. `ABC` reversed is `CBA` – epascarello Aug 13 '18 at 21:02
  • 2
    `reverse` reverses the array. What you're doing in the second line of the function is to take "))" and splitting it into an array [")", ")"], then reversing this array, which is [")", ")"]. Reverse does not "reverse" the string characters to it's conceptual counterparts. Hope this helps you on the right track (or perhaps off the wrong one at least). – tomahaug Aug 13 '18 at 21:03
  • https://stackoverflow.com/questions/14813369/palindrome-check-in-javascript – Pavlo Aug 13 '18 at 21:03
  • how about `<()>`? you need that too? – Tomasz Bubała Aug 13 '18 at 21:05
  • <><> is ok, so is <()> – totalnoob Aug 13 '18 at 21:07
  • You're going to have to define your set. `A` could be considered the "opposite" of `Z`, `|` could be considered the opposite of itself. I imagine you're probably wanting an automated approach, but that's not likely to happen. The only correlation you could make is that `[` probably has a close numeric, ascii value to `]`, but so does `A` and `B`. What specific set of characters are you targeting? – Joseph Marikle Aug 13 '18 at 21:10
  • I see, @TomaszBubała, reverse only reverses the order of the items. – totalnoob Aug 13 '18 at 21:10

3 Answers3

1

Store the opposites in an object. There is no other way to determine that < is an opposite of > etc. Then run your string through a function that takes half of your string and checks if the other half (in correct order) is made up of its opposites. Note that below solution works if length is even.

const opposites = {
  "(": ")",
  ")": "(",
  "<": ">",
  ">": "<",
  "[": "]",
  "]": "["
};
function isMirrored(str) {
  const half = str.slice(0, str.length / 2);
  const reversed = half.split("").reverse().join("");
  let mirrored = half;
  for(char of reversed) {
    mirrored += opposites[char];
  }
  return str === mirrored;
}
console.log(isMirrored("[[]]"));
console.log(isMirrored("<<>>"));
console.log(isMirrored("<()>"));
console.log(isMirrored("([]("));
console.log(isMirrored("(())"));
Tomasz Bubała
  • 2,093
  • 1
  • 11
  • 18
1

const REVERSED = {
  "(": ")",
  "<": ">",
  "[": "]",
}

function ifEqual(str) {
  let left = str.slice(0, str.length / 2).split('');
  let right = str.slice(str.length / 2, str.length).split('');
  
  return left.every((next, index) => REVERSED[next] == right[right.length - index - 1])
}

console.log(ifEqual("(())"))
console.log(ifEqual("<(>>"))
console.log(ifEqual("<[[(())]]>"))
sellmeadog
  • 7,437
  • 1
  • 31
  • 45
0

(()) is not a palindrome, no matter how symmetrical it may look. Don't let the optical illusion deceive you. Bellow is a common implementation of the palindrome checker algorithm in JavaScript.

function ifEqual(str) {
    return str === str.split("").reverse().join("")
}
console.log(ifEqual("(())"))
Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
Shakil Ahmed
  • 181
  • 1
  • 3
  • 13