/    Sign up×
Community /Pin to ProfileBookmark

Past Exam Question

I took an exam earlier this morning and there were loads of javascript questions, i managed to answer them all with the exception of just one. I took home the exam paper and am very interested in finding out how to actually do it.

The question is:

[CODE]“Without using events, delays or moving the layout of the existing code, change both DIV’s so they are not displayed. Use the following code and answer in the marked places”[/CODE]

[CODE]<html>
<head></head>

<body>
<div id=”div1″ style=”border:2px solid red;width=300px;margin:5px;”>
Above the code
</div>

<!– Answer A –>
<script language=”javascript” type=”text/javascript”>

</script>

<!– Answer B –>
<script language=”javascript” type=”text/javascript”>

</script>

<div id=”div2″ style=”border:2px solid blue;width=300px;” >
Below the Code
</div>

</body>
</html>[/CODE]

I added these two lines to the code sections
Answer A: document.getElementById(“div1”).style.display = ‘none’;
Answer B: document.getElementById(“div2”).style.display = ‘none’;

When the code was rendered in a browser div1 had disappeared, but div2 had not, as the javascript code is actually before the HTML code.

Any Ideas?

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMay 21.2009 — I think this is either a mistake, or they want you to use some additional DOM functions. In answer b, you could create a SCRIPT element. Make the innerHTML property of the new SCRIPT node the JavaScript required to hide the second DIV, then append that SCRIPT node to the <body> node. That [I]might[/I] work. You also could use JavaScript to alter a style sheet on the page to add a [B]#div2 { display: none; }[/B] style declaration to it.
Copy linkTweet thisAlerts:
@FangMay 21.2009 — or use the onload event to hide div2
Copy linkTweet thisAlerts:
@CharlesMay 21.2009 — or use the onload event to hide div2[/QUOTE]That was my thought and I'm thinking that's what the teacher is after. But we're no allowed to use an event handler. Next best would be to use the DOM to add a style element up above. Or we could violate the rules use [font=monospace]document.write ('<div style="display:none">')[/font] or we could creatively understand the question and use [font=monospace]document.getElementsByTagName ('BODY')[0].style.display = 'none'[/font]
Copy linkTweet thisAlerts:
@prt20authorMay 21.2009 — I think this is either a mistake[/QUOTE] I thought that at first, and thought it was a trick question.



or use the onload event to hide div2 [/QUOTE] Thought about this one too, but can not use events![/QUOTE]


document.getElementsByTagName ('BODY')[0].style.display = 'none'[/QUOTE] This is an interesting way of doing it..... but it would make the whole <body> invisible!

But the Script code should always be declared at the top anyway shouldnt it? so this question is wrong!!!!! maybe i should of pointed that out too
Copy linkTweet thisAlerts:
@toicontienMay 21.2009 — But the Script code should always be declared at the top anyway shouldnt it? so this question is wrong!!!!! maybe i should of pointed that out too[/QUOTE]
You can put <script> tags in the head or body. Some people prefer them in the head, others at the end of the body.

When placed in the head, the browser pauses the page rendering process while the script downloads and executes, causing a blank white page to be visible to the user during this time.

When placed in the body, the browser pauses the rendering process at the point the script was encountered, so depending on where the <script> is, the user might see a partially rendered page for a moment.

When placed as the last tags in the body, the full page has been rendered and the rendering process pauses while the scripts are downloaded and executed. The advantage here is that the entire page is visible to the user, giving them something to look at while your scripts download and execute.

As to the right way, it's really your preference. Each way has advantages and disadvantages.
Copy linkTweet thisAlerts:
@CharlesMay 21.2009 — This is an interesting way of doing it..... but it would make the whole <body> invisible! [/QUOTE]Exactly. Your task was to hide the DIV but you weren't instructed to hide only the DIV.

And it's the BODY not the <body>. Since the tags are optional and the element will be there in any case it is standard to refer to the element by its name and not by its start tag.
Copy linkTweet thisAlerts:
@Declan1991May 21.2009 — Exactly. Your task was to hide the DIV but you weren't instructed to hide only the DIV.[/QUOTE]

It does state to "change both divs", I don't think that your solution would actually be correct in that case. It would have to say "hide both divs" to allow that.
Copy linkTweet thisAlerts:
@CharlesMay 21.2009 — It does state to "change both divs", I don't think that your solution would actually be correct in that case. It would have to say "hide both divs" to allow that.[/QUOTE]You obviously were not a Philosophy major. "Hide both DIVs" would require it, "Hide DIVs separately" would disallow it, "Hide the DIV" allows it". The words "accomplish this" don't mean "accomplish only this" otherwise we would never need to use that second set of words.
Copy linkTweet thisAlerts:
@prt20authorMay 21.2009 — Exactly. Your task was to hide the DIV but you weren't instructed to hide only the DIV[/QUOTE]

actually!!!! I didnt write the question out correctly if you see in the code there are areas for two answers. The question reads, a) turn off the display for div1, question b) turn off the display for div2.

But regardless of this, i do not like when exam questions mark you on the basis of how you interpret the question, of course this is needed sometimes, but it is still very sneaky. I want to show what i have learnt, not how well i can read a question, but thats what exams are about arent they!
Copy linkTweet thisAlerts:
@CharlesMay 21.2009 — I was a Philosophy major and so those types of questions are my favorite. And it's not really a trick question; you're the one adding things. It all comes down to using language in a very exacting way--which is what programming is all about.
Copy linkTweet thisAlerts:
@Declan1991May 21.2009 — "Hide both DIVs" would require it, "Hide DIVs separately" would disallow it, "Hide the DIV" allows it".[/QUOTE]
The original (i.e. what was originally posted) doesn't read as that, it says "change both divs", your solution only changes both divs by inheritance, which I don't think counts.

I would also point out that some of your "precise meanings" are arbitrary, they are what you assign to those words. "Nice" is the typical example. The nuance implied by a particular phrase does not necessarily mean that another person should or will infer the same. Descriptivism is not the way language works, that is widely recognised nowadays.
Copy linkTweet thisAlerts:
@David_HarrisonMay 22.2009 — creatively understand[/QUOTE]Haha, love it. ?

I would have to say that the teacher expects you to add some styles in the head section to hide the second div, and that is why the very minimalist page includes a head. Annoying it includes neither a DOCTYPE nor a title though, so I'd probably have added a third div with a comment to that effect.
×

Success!

Help @prt20 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...