/    Sign up×
Community /Pin to ProfileBookmark

Help with Javascript quiz

I need help with these 3 questions with this javascript quiz for a job. I didn’t get the job but would like to find out how it’s done so I can learn. Can anyone help?

Question 1
Write a Javascript class with the following specifications:
1) Class name “fruit”
2) Class takes in an argument “fruitName”
3) a private variable “name” that is set when the class is instantiated
4) a public method “getName” that returns variable “name”

Question 2
Write a piece of Javascript code (jQuery allowed) that finds the various fruit names in the HTML
structure below and stores them in an array variable “fruits”:
<ul id=”fruits”>
<li> Apple </li>
<li> Banana </li>
<li> Orange </li>
</ul>

Question 3
Write a JSON representation of the the variable “fruits”.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mityaMar 18.2011 — Hmm, I think the person that wrote this test was a C or Java programmer, since the terms used are not entirely compatible with Javascript. JS CAN simulate object-oriented programming, but in its own way. As such there are some discrepancies:

[B]Question 1:[/B]

The only 'private' variables in Javascript are those prefixed with 'var' inside functions and methods. But in order that each instance of the Fruit class can have a different name, the variable needs to be declared on the instance - i.e. 'this' - not on the method itself, with var. But the former method will NOT be private - it will be globally accessible.

Also, the test says to set the variable but does not say what its assigned value should be. I assume it should be the value passed into fruitName.

[CODE]function Fruit(fruitName) {
this.name = fruitName;
Fruit.prototype.getName = function() { return this.name; }
}[/CODE]



[B]Question 2:[/B]

It's unclear whether he wants you to first declare the fruits to look for, then find them, or simply assume that the textual content of each LI is a fruit. I assume the latter. I also assume he doesn't want you to trim whitespace from the side of the fruits.

[CODE]$(function() {
var fruits = [];
$('#fruits li').each(function() { fruits.push($(this).text()); });
alert(fruits);
});[/CODE]



[B]Question 3:[/B]

Not clear if they want you to create a JSON object or actually print one out. I assume the former:

[CODE]var output = '{';
for(var s=0; s<fruits.length; s++) output += s+': "'+fruits[s]+'",';
output = output.substr(output, output.length-1)+'}';
alert(output);[/CODE]


If you want me to explain the steps in any of the above, just ask.
Copy linkTweet thisAlerts:
@bpm125authorMar 18.2011 — Thanx a lot! Question: How do the functions to questions 1 and 2 work?
Copy linkTweet thisAlerts:
@mityaMar 18.2011 — The function in question 1 is JS's simulation of a class. If you're unsure of the difference between functions and classes, I'd suggest looking that up. It is 'instantiated' - i.e. an instance of it is created (of which there may be many) by assigning it to a variable and calling it with the 'new' keyword.

'getName()' is a method OF the Fruit() class. The strange prototype thing you see has to do with the fact that JS implements what is called prototypal inheritance (something else to look up). In this case, we want each instance of the class to have a different fruit name - hence it has to be done this way. 'this' refers to the instance (a key concept of object-oriented programming).

In question 2, we use a jQuery selector to match the UL and its LI children, then we iterate over them. For each, we store the text value of the LI currently being looked at - i.e. the fruit - in our array. The whole thing runs inside a 'document ready handler', which means the code won't execute until the DOM has loaded (a requirement any time your JS is manipulating or interrogating the DOM).
Copy linkTweet thisAlerts:
@bpm125authorMar 18.2011 — Thanx a lot for your help! I really appreciate it!
×

Success!

Help @bpm125 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.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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