title: Alternate capitalisation in a string
body: <p>I would like to create a function which alternates letter capitalisation. For example, <code>Hello World</code> would become <code>HeLlO wOrLd</code>. The first letter of the string must always be capitalised.</p>
  
  <p>Here is the code I have written:</p>
  
  <pre><code>function alternatingCaps(str) {
      let alternate = str.charAt(0).toUpperCase();
      for(let i = 1; i &lt; str.length; i++) {
          let previousChar = str.charAt(i - 1);
          if(previousChar === previousChar.toUpperCase())
              alternate += str.charAt(i).toLowerCase();
          else if(previousChar === previousChar.toLowerCase())
              alternate += str.charAt(i).toUpperCase();
      }
      return alternate;
  }
  </code></pre>
  
  <p>I declared the <code>alternate</code> variable with the capitalised first character of the supplied string. I then loop through the rest of the string and check if the character preceding the current iteration is uppercase or lowercase; whichever it is, the current letter will become the opposite.</p>
  
  <p>However, this does not have the desired outcome. Here are a couple of tests and their corresponding results:</p>
  
  <pre><code>console.log(alternatingCaps('hello world'));
  // Output: "HELLO wORLD"
  
  console.log(alternatingCaps('jAvaScrIPT ruLEZ'));
  // Output: "JAvAScRIpt rULez"
  </code></pre>
  
  <p>How do I fix my function?</p>