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("(())")

11
  • 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 Commented Aug 13, 2018 at 21:00
  • 2
    @BrettBeatty "(())" is not a palindrome, nor is "<<>>" Commented Aug 13, 2018 at 21:01
  • More information is needed: is <><<>> okay? is <><> okay? Will only characters that have "flips" be used? Commented Aug 13, 2018 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. Commented Aug 13, 2018 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). Commented Aug 13, 2018 at 21:03

3 Answers 3

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("(())"));

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

Comments

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("<[[(())]]>"))

Comments

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("(())"))

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.