As part of studying how to use switch, I wrote this .js script that's supposed to obtain user input, evaluate whether the user input a number, a string or mixed output, and then return the appropriate output. The prompt() command works fine but the alert() commands produce empty output. I'm not sure where the problem is.

// Request user input
var inputString = prompt("Type in a number or some words, but not both.");

// Declare variables
var outputString = ""
var switchString = ""

// Declare function that evaluates user input
var eval = function(outputString)   {
    for (i = 0; i < inputString.length - 1; i++) {
        if (isNaN(inputString.substring(i, i + 1)) !== isNaN(inputString.substring(i + 1, i + 2)))  {
            switchString = "one";
            return "You typed in a combination of numbers and letters! Bad person!";
        }
        else if (isNaN(inputString))    {
            switchString = "two";
            return "You typed in some words. Thanks!";
        }
        else    {
            switchString = "three";
            return "You typed in a number. Thanks!";
        }
    }
}

// Call function
eval();

// Switch statement
switch (switchString)   {
    case 'one':
        alert(outputString);
        break;
    case 'two':
        alert(outputString);
        break;
    case 'three':
        alert(outputString);
        break;
    default:
        alert("This program is buggy and doesn't work!");
}

eval is an existing function name. Rename your function.

Okay, there are two problems I've found. The first is that the following combination of code doesn't work:

var evalFunc = function()    {
    expression
    return "You typed in what I didn't ask you to.";
};

evalFunc(outputString);

alert(outputString);

The alert() function returns a blank output within the alert box that does get displayed in my browser. This might be a problem with my Firefox Scratchpad, as the Execute>Display command returns /* undefined */ within the Scratchpad itself, the same as console.log() does.

The other problem is this bit of code:

if (isNaN(inputString.substring(i, i + 1)) !== isNaN(inputString.substring(i + 1, i + 2)))  {
            switchString = "one";
            outputString = "You typed in a combination of numbers and letters! Bad person!";
            return;

When modified this way for other expressions, alert(outputString) does display outputString within the alert() box in my browser window. The if condition, however, causes Scratchpad to return this error message:

/*
Exception: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMWindow.alert]
@38
*/

So is this just a matter of my needing a proper IDE in order to learn how to develop Javascript code, as opposed to coping with bugs in Scratchpad, or is something actually wrong with my code?

Checking your code from your first post, I find these errors and problem:

var inputString = prompt("Type in a number or some words, but not both.");

// Don't forget the semicolon, it's a good practice on putting one
// at the end of each statement
var outputString = ""
var switchString = ""

// a. You're using the keyword 'eval' which is used to run
// scripts in a string
// b. you passed in an empty value, if what you're doing is passing outputString
// from the global context. If that's what you;re doing, all primitive type, except for objects
// are passed-by-value, but, if you passed in an object, it's the pointer to that object will be
// passed. Another thing is, setting an 'outputString' as an argument name in your function
// creates another variable of that same name in the function context.
var eval = function(outputString)   {
    for (i = 0; i < inputString.length - 1; i++) {
        // how will you know if it's one, two or three?
        if (isNaN(inputString.substring(i, i + 1)) !== isNaN(inputString.substring(i + 1, i + 2)))  {
            switchString = "one";
            return "You typed in a combination of numbers and letters! Bad person!";
        }
        else if (isNaN(inputString))    {
            switchString = "two";
            return "You typed in some words. Thanks!";
        }
        else    {
            switchString = "three";
            return "You typed in a number. Thanks!";
        }
    }
}

// don't use the keyword eval. also, if you're calling the function you created
// it's extected to return a value.
eval();


// Switch statement
switch (switchString)   {
    case 'one':
        alert(outputString);
        break;
    case 'two':
        alert(outputString);
        break;
    case 'three':
        alert(outputString);
        break;
    default:
        alert("This program is buggy and doesn't work!");
}

You should check on those first. If you wanted to play with the switch statement using prompt, I would advice to start with this. Then make it complex once you get a hang on it.

switch(prompt("input a number")){
    case '1': 
        alert("Your inputted '1'!");
        break;
    case '2': 
        alert("Your inputted '2'!");
        break;
    default:
        alert("I currently don't know what you've just inputted!");
        break;
}

You can use JSFiddle
It's sexier than Scratchpad. LOL

Thanks for helping out. It really is appreciated.

Maybe it's because it's 4:30 in the morning and I'm just having my first coffee, but this paragraph is plugging up my eyeball pupils and making me blind:

// b. you passed in an empty value, if what you're doing is passing outputString
// from the global context. If that's what you;re doing, all primitive type, except for objects
// are passed-by-value, but, if you passed in an object, it's the pointer to that object will be
// passed. Another thing is, setting an 'outputString' as an argument name in your function
// creates another variable of that same name in the function context.

I'm going to drink some more coffee until that paragraph starts to look less like spoken Sanskrit and more like English. ... Not that you did anything wrong. It's just that I'm feeling extry-stupid right now.

I'll be back after some appropriate self-destupidification. Again, thanks for your help.

LOL.
I'm the one who need the self-destupidification and needs a tank of coffee. haha
Anyway, I think, the way I structured that paragraph was way too incomprehensible.

To make it up, here's an example of what I meant:
http://jsfiddle.net/cXSF8/

Here's the code from the example, but it would be more appreciable if seen when run.

var objectValue = {variableToChange: 5},
    numberValue = 5;

function valueChanger(changedVariable){
    if(typeof(changedVariable)=='object'){
        changedVariable.variableToChange = 10;
    } else {
        changedVariable = 10;
    }
}


document.write("<b>BEFORE Any Changes</b><br />");
document.write("Value of 'variableToChange' in objectValue: <b>" + objectValue.variableToChange + "</b><br />");
document.write("Value of 'numberValue': <b>" + numberValue  + "</b><br />");

document.write("<b>AFTER Any Changes</b> <i>Function 'valueChanger' makes the value 10</i><br />");
valueChanger(objectValue.variableToChange);
document.write("Value of 'variableToChange' in objectValue (passing only the attibute): <b>" + objectValue.variableToChange + "</b><br />");
valueChanger(objectValue);
document.write("Value of 'variableToChange' in objectValue (passing the whole object): <b>" + objectValue.variableToChange + "</b><br />");
valueChanger(numberValue);
document.write("Value of 'numberValue': <b>" + numberValue  + "</b><br />");

Let me know if you have any questions

gon,

I'm just starting to learn about objects in javascript, so I'm going to complete my lessons on objects and then study your code carefully. Thanks for posting it.

Eyetee

Great. Just let me know if you need clarifications, I'll be watching this thread.

Here's a good reference for JavaScript:
Learn javascript

It's the best place to learn JavaScript - for me.

Have fun learning.

Hi, gon,

Since you say you're watching this thread, I thought I'd ask you about something. See this code:

var myName = 'Eyetee';
var myAge = 0;

var me = {
    me.name: myName,
    me.age: myAge
};

var meToo = {};
meToo.name = myName;
meToo.age = myAge;

var meThree = new Object();
meThree.name = myName;
meThree.age = myAge;

I'm being taught that all three ways of declaring and defining objects are valid, but, for some reason, the first one (for the me{} object) gets rejected by the command interpreter I'm using as part of the training. Is it not a valid structure, or is the command interpreter just buggy?

Also, I'm being taught that I can also use this way to list key-value combinations:

meFour['name'] = myName;

In case the command interpreter is buggy, is that accurate information?

Regards,

Eyetee

Hi Eyeteeorg,

Yes I am. :)

var me = {
    me.name: myName,
    me.age: myAge
};

This one's not valid. When defining an object literal, the property name should either be a word with no space or, if it has one, atleast contain it in a primitive string wrapper like this one:

var me = {
    name: "Makoto",
    "age": 76,
    "full name": "Aoki Makoto"
};

If you want to access the "full name" property use a bracket notation, an example is this one, me["full name"]. Since "full name has a space on it, it won't be possible to use the dot notation when accessing properties; the parser will see it as a syntax error.

Hope you're enjoying the holidays, gon. Here are some questions:

var objectValue = {variableToChange: 5},

What does the :5 part of the parameter do?

numberValue = 5;
function valueChanger(changedVariable){
if(typeof(changedVariable)=='object'){

What is the typeof() function?

changedVariable.variableToChange = 10;
} else {
changedVariable = 10;
}
}
document.write("<b>BEFORE Any Changes</b><br />");

Is document. a module or something else? What is the write() function? What does the <b></b> code do within the quotation marks? And what does <br /> do--I'm not familiar with that structure.

document.write("Value of 'variableToChange' in objectValue: <b>" + objectValue.variableToChange + "</b><br />");
document.write("Value of 'numberValue': <b>" + numberValue + "</b><br />");
document.write("<b>AFTER Any Changes</b> <i>Function 'valueChanger' makes the value 10</i><br />");
valueChanger(objectValue.variableToChange);
document.write("Value of 'variableToChange' in objectValue (passing only the attibute): <b>" + objectValue.variableToChange + "</b><br />");

What does "passing only the attribute" do?

valueChanger(objectValue);
document.write("Value of 'variableToChange' in objectValue (passing the whole object): <b>" + objectValue.variableToChange + "</b><br />");

What does "passing the whole object" do?

valueChanger(numberValue);
document.write("Value of 'numberValue': <b>" + numberValue + "</b><br />");

I still haven't run your script to see how it works, but once I've heard from you on those preliminary points, I shall.

Regards,

Eyetee

PS: Please go to my website and send me an email through the link there. I want to ask for your input on a real-life solution that a friend asked me to implement--my very first one ever!

Yes, I am. Been drinking non-stop yesterday and the other day. Enjoy life to the fullest. LOL
Thanks. Happy holidays Eyetee, enjoy the last days of 2012. :)

var objectValue = {variableToChange: 5},
1. What does the :5 part of the parameter do? Assign the number five to object objectValue's variableToChange property. It's equivalent to this objectValue.variableToChange = 5;

numberValue = 5;
function valueChanger(changedVariable){
    if(typeof(changedVariable)=='object'){

2. What is the typeof() function? Get the type of a given variable type. It will return either of the following: string, number, function, object, or undefined. All returned value are in string, and lower-cased.

        changedVariable.variableToChange = 10;
    } else {
        changedVariable = 10;
    }
}
document.write("<b>BEFORE Any Changes</b><br />");

3. Is document. a module or something else? What is the write() function? What does the <b></b> code do within the quotation marks? And what does <br /> do--I'm not familiar with that structure. *(a)Yes, it's used for manipulation and info gathering from the HTML. (b)It's an HTML tag to make lines or in-line characters bold. (c)It's an XML style tag, for HTML's breakline, an alias to "<br>". *

document.write("Value of 'variableToChange' in objectValue: <b>" + objectValue.variableToChange + "</b><br />");
document.write("Value of 'numberValue': <b>" + numberValue + "</b><br />");
document.write("<b>AFTER Any Changes</b> <i>Function 'valueChanger' makes the value 10</i><br />");
valueChanger(objectValue.variableToChange);
document.write("Value of 'variableToChange' in objectValue (passing only the attibute): <b>" + objectValue.variableToChange + "</b><br />");
valueChanger(objectValue);
document.write("Value of 'variableToChange' in objectValue (passing the whole object): <b>" + objectValue.variableToChange + "</b><br />");

4. What does "passing only the attribute" do? What does "passing the whole object" do? *(a) Passess only the objectValue.variableToChange to the function valueChanger. (b) Passes the wholeobjectValue to the function valueChanger. *

It will be better for your fourth question to play with the link I gave you. Play with the code, change it if you want too and see the effects right away. Here's it again if you missed it: Examples

Sure, I'll drop you an email, I'm not gonna do anything much the next 3 hours anyways. Man, these are the same reasons why we love and hate holidays. LOL

I still have a ton of stuff to learn. Thank you so much for your extensive help. I promise it's not going to go go waste. And yes, I'll copy the code block to a file and then run it to see what it does, as well as changing it to see what effects the changes have. Thanks for giving me permission to do so.

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.