Click here to Skip to main content
15,794,275 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I'm struggling with JS, as you can see. Is it okay to use 2 parameters in a typeof? Also, will using 'if (!output) return false;' actually work to Verify that corresponding element is found? The program "works" (displays an answer), but I just want to be certain that the error checks are correct. I hope that made sense! I'm going to paste the whole code, just in case:

// today.js #2
// This script indicates the current date and time.

// This function is used to update the text of an HTML element.
// The function takes two arguments: the element's ID and the text message.
function setText(elementId, message) {
    'use strict';
    // Updated arguments so they cannot be an empty string - bullet #1
    // Updated arguments so they can also be a number - bullet #2
    if ( (typeof elementId == 'string' || Number)
            && (typeof message == 'string' || Number) )
    if ( (typeof elementId !== '')
            && (typeof message !== '') ){
        // Get a reference to the paragraph:
        var output = document.getElementById(elementId);
        //Verify that corresponding element is found - bullet #3
        if (!output) return false;
        // Update the innerText or textContent property of the paragraph:
		if (output.textContent !== undefined) {
			output.textContent = message;
		} else {
			output.innerText = message;
    } // End of main IF.

} // End of setText() function.

// Call this function when the page has loaded:
function init() {
    'use strict';
    var today = new Date();
    var message = 'Right now it is ' + today.toLocaleDateString();
    message += ' at ' + today.getHours() + ':' + today.getMinutes();

    // Update the page:
    setText('output', message);
} // End of init() function.
window.onload = init;

Well, I'm in over my head here. This is from the textbook that I am using:

Validate the function’s parameters:
if ( (typeof elementId == ‘string’)
&& (typeof message == ‘string’) ) {
This function can only work if it receives both values and both are of type
String (well, technically, the message could be a number).

Then the assignment was to:
Update today.js so that message can also be a number. And to update it so that it can't be an empty string.

I used this for the empty string:

if ( (typeof elementId !== '')
            && (typeof message !== '') )

but no amount of research gave me an indication of how to add so that the message could be a number. This is my first month, 3rd assignment in JS, so I thank you for any guidance that you can give me!
Updated 23-Feb-15 10:45am
Maciej Los 23-Feb-15 14:09pm    
The question makes no sense...
Sergey Alexandrovich Kryukov 23-Feb-15 14:54pm    
You are right. At the same time, some part of this question makes certain sense, so I provided detailed answer. There are few aspects to pay attention for.
Please see Solution 1.

1 solution

Of course there cannot be "2 parameters in a typeof". One simple reason is this: typeof is, syntactically, operand, not a function, so, formally, it can have operand, no "parameters". Only one operand, no ifs no buts:[^].

Moreover, having more then this only operand cannot make any sense.

The question makes no sense in the following sense: code can be right or wrong only if its purpose is known. Of course, it should be, in case of JavaScript, lexically correct. One wring comma or semicolon can make whole script invalid, not just the point with the error. (JavaScript is not the language which is executed one by one, as many mistakenly think; if the code has an error on lexical level, nothing executes, not even some correct part of it.)

Now, here is one problem with the type check you perform: for example, let's assume that some object is, say, string, but you could misspell the word 'string'. The check will always fail, even for strings, and nothing can tell you that the real reason is misspelled type name. So, I devised the following technique:
if (typeof message == typeof '') { /* ... */ }

[EDIT] I'll be very glad to see if someone suggests something better. [END EDIT]

At the same time, in nearly all cases, development should not rely on the type check. After all, JavaScript has too few types distinguished on this level. Even though I actually use the technique shown above, I only do it in some special cases, such as in my string formatting or object dump methods. It's better to rely on techniques where the type is exactly known.

For objects of the type 'object', you can check up that the two objects have the same origin checking up their constructors:
var obj1 = [1, 2, 3];
var obj2 = [4, 5];
var ofTheSameType12 = obj1.constructor === obj2.constructor; // true

function MyObject(id) { = id; this.comment = 'some comment'; }
var obj3 = new MyObject(1);
var obj4 = new MyObject(200);
var ofTheSameType34 = obj3.constructor === obj3.constructor; // true
// true, despite the fact that obj3 and obj4 are logically different

function MyOtherObject(id) { = id; this.comment = 'some comment'; }
var obj5 = new MyObject(1);
var obj6 = new MyOtherObject(1);
var ofTheSameType56 = obj5.constructor === obj6.constructor; // false
var ofTheSameType56NonStrict = obj5.constructor == obj6.constructor; // false
// false, despite the fact that obj5  and obj6 are logically identical

Share this answer
Maciej Los 23-Feb-15 14:56pm    
Sergey Alexandrovich Kryukov 23-Feb-15 15:01pm    
Thank you Maciej.
This is because I've spent some time on JavaScript development lately. Have you seen my last article in "Game Development" section, in pure HTML+JavaScript+Canvas? It has fully playable life preview now, right on the article page, thanks to the enthusiasm of Chris Maunder about this part; added today. Practically, my very first game and first more or less complete JavaScipt work, by the way...

I have one more planned to publish.

Maciej Los 23-Feb-15 15:07pm    
I'm going to see it ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900