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);

Recommended Answers

All 2 Replies

Member Avatar for stbuchok

Because you can use either double or single quotes for strings it is fine to use either, however I tend to prefer to use single quotes for strings in JavaScript as you will find that you are writing html strings. (var html = '<input type="text" value="test" />' instead of needing to escape the double qutoes.)

Hi, stb,

Let me see whether I understand:

Because you can use either double or single quotes for strings it is fine to use either, however I tend to prefer to use single quotes for strings in JavaScript as you will find that you are writing html strings. (var html = '<input type="text" value="test" />' instead of needing to escape the double qutoes.)

When coding in html, you can use single quotation marks to enclose a literal, and any double quotation marks within that literal don't require escaping; but if you use double quotation marks to enclose the literal, you need to escape any double quotation marks within the literal to avoid having the html intepreter take them as representing the end of the literal. Have I got that right?

Suppose you use double quotation marks to enclose the literal and single quotation marks for any inner content. Would that work in html?

Also, when it comes to javascript, do you need to escape double quotation marks within a literal if you use single quotation marks to enclose it. (I already know from having tried it that, in javascript, if you enclose your literal with double quotation marks, the interpreter has no problem with apostorphes inside the literal, and single quotation marks = apostrophes.)

I guess what I really need to know is some theory, such as how the html interpreter relates to the javascript interpreter. If you call a .js script from within html code, does the html interpreter have to interpret the .js code and risk getting confused, or does the javascript interpreter handle .js scripts separately?

Regards,

Eyetee

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.