I'm just cutting my teeth on a few play projects in order to get better at javascript coding. Kindly have a glance at the following and comment on my general coding practices, as I want to get into the best habits right off the bat and break any bad habits I might be developing.
(Note that the anagramFunc() function is still under development and will be a challenge for me, but I'm not asking for help with it yet. I want to try to figure it out on my own.)
/* Eyetee's Toybox Version 0.0. Copyright 2012 by Eyetee and not released. All rights reserved.
Any redistribution, non-personal use, alteration of the code for non-personal purposes, or
commercial use or alteration of the code whatsoever are strictly prohibited without the
express written permission of the copyright holder. */
// Declare and define variables
var introStatement = "Welcome to Eyetee's Toybox! This program lets you play around with something you type in. \
Click OK to continue or Cancel to exit.";
var firstUserPrompt = "To start, type in something. It can be anything.";
var secondUserPrompt = "What do you want to do with what you just typed in: (1) print it backwards \
(2) print it jumbled (create an anagram); (3) print it in all capitals; (4) print it in all small \
letters; (5) find out how long it is? Type in the number you want and click OK or Cancel to exit.";
var errorMessage = "Sorry, but you didn't choose a number.";
var exitMessage = "Thank you for using Eyetee's Toybox. Have a nice day!";
var userInput = "";
var userChoice = "";
var userOutput = "";
// Declare and define functions
var reverseStringFunc = function() { // returns string in reverse order
if (userInput.length > 1) {
for (i = userInput.length - 1; i >= 0; i--) {
userOutput = userOutput + userInput.substring(i, i + 1)
}
}
else {
userOutput = userInput
}
};
var anagramFunc = function() { // creates anagram of string--not coded yet!
userOutput = "under construction";
};
var uppercaseFunc = function() { // returns string in uppercase
userOutput = userInput.toUpperCase();
};
var lowercaseFunc = function() { // returns string in lowercase
userOutput = userInput.toLowerCase();
};
var stringLengthFunc = function() { // returns string length
userOutput = UserInput.length;
};
// Print introduction
confirm(introStatement);
// Obtain user input
userInput = prompt(firstUserPrompt);
userChoice = prompt(secondUserPrompt);
// Main switch function
switch (userChoice) {
case '1': // prints user input backwards
reverseStringFunc();
userOutput = "What you typed printed backwards is " + userOutput;
break;
case '2': // creates anagram of user input
anagramFunc();
userOutput = "The anagram (jumble) of what you typed in is " + userOutput;
break;
case '3': // prints user input in uppercase
uppercaseFunc();
userOutput = "What you typed in, in uppercase, is " + userOutput;
break;
case '4': // prints user input in lowercase
lowercaseFunc();
userOutput = "What you typed in, in lowercase, is " + userOutput;
break;
case '5': // returns user input length in characters
stringLengthFunc();
userOutput = "What you typed is " + userOutput + " characters long";
break;
default:
confirm(errorMessage);
};
// Provide output
confirm(userOutput);
// Print exit message
confirm(exitMessage);