-3

My whole goal was to write a loop that would take a string, count the letters and return two responses: one = "this word is symmetric" or two = "this word is not symmetric". However the code I wrote doesn't console anything out. Here's the code:

var arya = function(arraycount){

for (arraycount.length >= 1; arraycount.length <= 100; arraycount++) {

while (arraycount.length%2 === 0) {
    console.log("This is a symmetric word and its length is " + " " arraycount.length " units.");
    arraycount.length%2 != 0
    console.log("Not a symmetric word");
    }
}
}
arya("Michael");
Kyle
  • 5
  • 2
  • 4
    Symmetric? Do you mean a palindrome? – Matt Burland Jun 16 '15 at 20:28
  • 2
    When you say symmetric, do you mean [palindromatic](https://en.wikipedia.org/wiki/Palindrome#Characters.2C_words.2C_or_lines)? –  Jun 16 '15 at 20:28
  • So `arraycount` is a string when you first call the function, then you are looping until `arraycount.length <= 100` (which will never happen), and at the end of the loop you do `arraycount++` *to a string*. This will try to turn it into a number and result in `arraycount` being `NaN`. `NaN.length` is undefined, which is always `<= 100` so you have an infinite loop. And you won't enter the body of the `while` either because `Michael` has 7 letters, and after the first time through the loop `NaN % 2` is still `NaN` which is not `0`. – Matt Burland Jun 16 '15 at 20:33
  • there's also a problem with the while loop, which looks more like it should be a if test, and the second one arraycount.length%2 != 0..... i would suspect this code would throw a lot of console errors just being read – Daemedeor Jun 16 '15 at 20:35
  • What type is `arraycount` - a simope string? I don't believe you can use `arraycount.length` (and for that matter, `arraycount` itself) in such a way in a `for` loop. – Jongware Jun 16 '15 at 20:35
  • .. And what kind of statement is `arraycount.length%2 != 0`? – Jongware Jun 16 '15 at 20:36
  • And "count the letters" is a little ambiguous. What *exactly* do you mean? The length of a string is just `myString.length`, but that will include spaces and any other (non-letter) characters. – Matt Burland Jun 16 '15 at 20:39
  • possible duplicate of [Palindrome check in Javascript](http://stackoverflow.com/questions/14813369/palindrome-check-in-javascript) –  Jun 16 '15 at 21:27

1 Answers1

0

There are many ways to accomplish your goal, but here are a few. The first is a somewhat naïve approach using a for loop, and the second uses recursion. The third asks whether the string equals the reverse of the string.

iterative (for loop) function

var isPalindromeIteratively = function(string) {
  if (string.length <= 1) {
    return true;
  }
  for (var i = 0; i <= Math.floor(string.length / 2); i++) {
    if (string[i] !== string[string.length - 1 - i]) {
      return false;
    }
  }
  return true;
};

This function begins by asking whether your input string is a single character or empty string, in which case the string would be a trivial palindrome. Then, the for loop is set up: starting from 0 (the first character of the string) and going to the middle character, the loop asks whether a given character is identical to its partner on the other end of the string. If the parter character is not identical, the function returns false. If the for loop finishes, that means every character has an identical partner, so the function returns true.

recursive function

var isPalindromeRecursively = function(string) {
  if (string.length <= 1) {
    console.log('<= 1');
    return true;
  }
  var firstChar = string[0];
  var lastChar = string[string.length - 1];
  var substring = string.substring(1, string.length - 1);
  console.log('first character: ' + firstChar);
  console.log('last character: ' + lastChar);
  console.log('substring: ' + substring);
  return (firstChar === lastChar) ? isPalindromeRecursively(substring) : false;
};

This function begins the same way as the first, by getting the trivial case out of the way. Then, it tests whether the first character of the string is equal to the last character. Using the ternary operator, the function, returns false if that test fails. If the test is true, the function calls itself again on a substring, and everything starts all over again. This substring is the original string without the first and last characters.

'reflecting' the string

var reflectivePalindrome = function(string) {
  return string === string.split('').reverse().join('');
};

This one just reverses the string and sees if it equals the input string. It relies on the reverse() method of Array, and although it's the most expressive and compact way of doing it, it's probably not the most efficient.

usage

These will return true or false, telling you whether string is a palindrome. I assumed that is what you mean when you say "symmetric." I included some debugging statements so you can trace this recursive function as it works.


The Mozilla Developer Network offers a comprehensive guide of the JavaScript language. Also, here are links to the way for loops and while loops work in JS.