-1

How to reverse a word and check if it is a palindrome and if an empty string is passed in javascript.

here is what i came up with but not working as desired.

function reverseString(str) {
    return str.split("").reverse().join("");
}

am suppose to run it against a test, here is the test.

describe("Produce the reverse order of a word: ", function () {
  describe("Case for en empty string", function() {

    it("should return null for empty string", function() {
      expect(reverseString('')).toEqual(null);
    });

  });

  describe("Case for palindromes", function() {

    it("should return true for `anna`", function() {
      expect(reverseString('anna')).toEqual(true);
    });

    it("should return true for `NaN`", function() {
      expect(reverseString('NaN')).toEqual(true);
    });

    it("should return true for `civic`", function() {
      expect(reverseString('civic')).toEqual(true);
    });

  });

  describe("Case for normal words", function() {

    it("should return `skoob` for `books`", function() {
      expect(reverseString('books')).toEqual('skoob');
    });

    it("should return `nomolos` for `solomon`", function() {
      expect(reverseString('solomon')).toEqual('nomolos');
    });

    it("should return `csim` for `misc`", function() {
      expect(reverseString('misc')).toEqual('csim');
    });

  });

});
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
  • How is it not working exactly? – cs95 Jun 24 '17 at 19:34
  • Returning `null` is an awful api. `''` should return `false` in my opinion. Rename your function too. You want `reverseString` to do something other than ... reversing a string... so name it differently. – 000 Jun 24 '17 at 19:42

3 Answers3

3

The tests in the first two test groups are not testing things correctly.

Why should this be true?:

  expect(reverseString('')).toEqual(null);

or this?

  expect(reverseString('anna')).toEqual(true);

You never return null, nor true, but always a string. The reverse of the empty string is the empty string. It seems like you are thinking of another function that you have not included, which could be called isPalindrome. Its definition could be:

function isPalindrome(s) {
    return s.length ? reverseString(s) === s : null;
}

Now, in the tests of the first two groups, replace all calls to reverseString with isPalindrome and it should work.

trincot
  • 317,000
  • 35
  • 244
  • 286
0

You are reversing the string correctly, however you are not catching the other cases yet.

The tests are expecting that given '' the function should return null.

Given a palindrome tacocat the function should return true.

theRemix
  • 2,154
  • 16
  • 16
0

You need something like:

function isPalindrome(str) {
  if ((typeof str) === 'undefined' || str.trim().length === 0) {
    return false;
  }
  var rev = str.split('').reverse().join('');
  if (str === rev) {
    return true;
  }
  return false;
}

Your implementation reverses the string but other cases need to be handled. Another problem is with your tests. Your function should remain consistent across tests. It should not change its return type based on test. So, it should always return boolean or string. It seems your test have mixed values.

Anuj Yadav
  • 980
  • 11
  • 20