PDA

View Full Version : Javascript Exam question



hydamo
10-01-2008, 12:32 AM
I am a complete novice with regard to Javascript and I had an exam where I was asked the following questions. I am done with the exam but I couldn't find a few answers. Can some one help by explaining with answers.

Thanks!

Dan :-)


Question:

1)Write a one-line piece of JavaScript code that concatenates all strings passed into a function:

function concatenate(/*any number of strings*/) {
var string = /*your one line here*/
return string;
}


2) What do these two examples have in common?

Ex 1:
var obj = document.getElementById(’adiv’);
document.getElementById(’adiv’).ptr = obj;

Ex 2:
function assignClick() {
var el = document.createElement(’div’);
function handleClick() {
el.innerHTML = ‘clicked!’;
}
el.attachEvent(”onclick”, handleClick);
}

magicyte
10-01-2008, 12:46 AM
I know the answer to #1 (so I think):

I rewrote the function:


function concatenate(/*arguments*/)
{
var string = "";
for(i = 0; i < arguments.length; i++) // either arguments.length or this.arguments.length
{
string += arguments[i].value; // I doubt this works - might work
}
return string;
}

This may work. I haven't tested it, so it's up to you. Were the qustions multiple choiced? If so, what were the choices?

-magicyte

Nile
10-01-2008, 12:50 AM
Wrong magic, to make it work you would do this:


function concatenate(arguments)
{
var string = "";
for(i = 0; i < arguments.length; i++) // either arguments.length or this.arguments.length
{
string += arguments[i]; // I doubt this works - might work
}
return string;
}

And to call it you would need to make an array:


alert(concatenate(['Yo, ','Im connected']));

Also, I don't understand that question. But it wants ONE line... So idk.
On your second question, I'd have to say that there both messing with objects and elements.
Btw, if you know / can contact the person that made the quiz and ask:
Why they put one function in another function?

magicyte
10-01-2008, 12:54 AM
Wrong magic, to make it work you would do this:


function concatenate(arguments)
{
var string = "";
for(i = 0; i < arguments.length; i++) // either arguments.length or this.arguments.length
{
string += arguments[i]; // I doubt this works - might work
}
return string;
}


And to call it you would need to make an array:


alert(concatenate(['Yo, ','I'm connected']));


With your posted code, that would work. Now I can change my code. Thanks, Nile!

Here's the code:


function concatenate(txt)
{
var string = "";
for(i = 0; i < txt.length; i++)
{
string += " " + txt[i];
}
return string;
}

// Example:
alert(concatenate(['hi','who']);

Then again, you can use this code:


function concatenate(/* args */)
{
var string = "";
for(x = 0; x < arguments.length; x++)
{
string += " "+arguments[x];
}
return string;
}

// Example:
alert(concatenate('this','should','work'));

I'm very sure this will work.

-magicyte

Twey
10-01-2008, 04:36 AM
function concatenate() {
return Array.prototype.join.call(arguments, "");
}The thing both the pieces of code have in common is that they create a circular reference, and therefore a memory leak — in the first, obj -> ptr -> obj, and in the second, el -> closure -> onclick -> el.

Nile
10-01-2008, 01:16 PM
Hey, Twey:
What does ptr do?

Twey
10-01-2008, 04:40 PM
In this code, 'ptr' is a property attached to obj, which refers back to obj. It is arbitrary: it doesn't do anything special.