0

Beginner learner in Javascript here! I am doing a few exercises based on do-while loops. As with my other questions (this is my 2nd one!) while I am learning, I always try to challenge my curiosity. The exercise asked me to do come up with this code, which is works based on the criteria of the exercise:

var i = 0;
var animals = ["horse", "ox", "cow", "pig", "duck"];
do {
  if (animals[i] === "pig") {
    alert(animals[i] + " is at Index " + i + "!");
    break;
  }
  i++;
} while (i < animals.length); 

But I wanted to see if I could deliver it in a much more "user-friendly" and "proper way" to displaying the alert in which the first letter of the string would be capitalized. Of course, I know that I could have just capitalized the strings in the array but I wanted to challenge myself in making JS convert to strings in the array to a capitalized first letter in the alert, so I came up with this, which works when I ran it on jsfiddle:

var animals = ["horse", "ox", "cow", "pig", "duck"];
var i = 0;
do {
  if (animals[i] === "pig") {
    var firstChar = animals[i].slice(0,1);
    var otherChars = animals[i].slice(1);
    firstChar = firstChar.toUpperCase();
    otherChars = otherChars.toLowerCase();
    var properAnswer = firstChar + otherChars;
    alert(properAnswer + " is at Index " + i + "!");
    break;
  }
  i++;
} while (i < animals.length);

Now my question is, is there a way to simplify the code above?

Sorry if my question was long >< I wanted to set it up so you can understand my context! Thank you in advance!

  • If you refer to this related answer, there is a simpler method to capitalizing the first letter of a word. http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript – tiantang May 09 '16 at 03:21
  • *"is there a way to simplify the code above?"* - Do you mean just the capitalisation part, or the whole thing? A `do..while` loop isn't a good choice for processing an array, because it doesn't test the array `.length` until after processing the first item, so you could get an error if the array is empty. A `while` loop (as compared to `do...while`) is OK, but I'd use a `for` loop. – nnnnnn May 09 '16 at 03:26
  • I think he just wants to see a beginner-friendly yet cleaner way of doing things – vtange May 09 '16 at 03:29
  • @nnnnnn just the first character of the string that is found in the index. As for your second point, I am just following a textbook's exercise. I think the author is just practicing me on how to differentiate do-loops with while-loops and for-loops since I just finished those sections. But thanks for the information, I will keep that in mind! – joserferrer May 09 '16 at 03:30
  • @vtange That is exactly my goal! When I clicked **tiantang's** question-referral above, I couldn't really understand what to do and where in my code I will apply that. Please let me know if Stack Overflow wouldn't be a place to ask beginner questions as I would totally understand! – joserferrer May 09 '16 at 03:32
  • `var properAnswer = animals[i].charAt(0).toUpperCase() + animals[i].slice(1).toLowerCase()` is the one-line replacement for the first five lines inside your `if` statement. Or you can dispense with the `properAnswer` variable and just do it all in the alert, with `alert(animals[i].charAt(0).toUpperCase() + animals[i].slice(1).toLowerCase() + " is at Index " + i + "!");` – nnnnnn May 09 '16 at 03:33
  • @nnnnnn Amazing! Thank you! – joserferrer May 09 '16 at 03:38
  • 1
    You're welcome. By the way, beginner questions are welcome on Stack Overflow, but if you already have working code and you just wonder whether it can be improved, your question might be better placed at http://codereview.stackexchange.com/. Stack Overflow is more about solving problems, so if you have code that doesn't work you'd post questions about that here. Regarding the types of loops, yes, practice all three for now, but you'll probably find the `do..while` less useful in real-world situations. Anyway, happy coding. – nnnnnn May 09 '16 at 03:40

0 Answers0