So you think you wanna be a web developer... Fork this, update your copy with answers. They don't need to be precise - pseudo-code is fine in most cases. Some questions don't have correct answers. Submit your answers to ara@dojo4.com FROM: you@yeraaddy.com SUBJECT: [dojo4-quiz] quiz-1 BODY: https://gist.github.com/to-your-submission
==========================================================
- is this broken in html5? if so why? ==========================================================
<div id='teh_javascripts' />
teh answerz...
In HTML5 <div/> == <div> not <div></div>
'http://domain.com:4242/foo/bar/baz.html'
teh answerz...
require 'open-uri'
response = open('http://domain.com:4242/foo/bar/baz.html')
curl 'http://domain.com:4242/foo/bar/baz.html'
========================================================== 3. in any language you choose, write this to run in parallel
numbers = 20, 1
results = []
numbers.each do |number|
results.push( parallelize{ number * 2 } )
end
sum = reduce( results )
teh answerz...
numbers = 20, 1
results = []
threads = []
numbers.each do |number|
threads << Thread.new { results << number * 2 }
end
threads.each { |t| t.join }
sum = results.reduce(:+)
========================================================== 4. in any language you choose, write this to run in parallel on multiple cores
numbers = 20, 1
results = []
numbers.each do |number|
results.push( parallelize{ number * 2 } )
end
sum = reduce( results )
teh answerz...
Seems like it depends a lot on the Ruby interpreter, but I suspect that using thread local variables, as follows, would make it more likely.
numbers = 20, 1
results = []
threads = []
numbers.each do |number|
threads << Thread.new { Thread.current['result'] = number * 2 }
end
sum = threads.reduce(0) { |sum,thread| thread.join ; sum + thread['result']}
<label for='user.email'>
<input name='user.email'>
teh answerz...
"for" needs to refer to an element id, not a name.
if @not_modified
render :status => status
end
teh answerz...
Without additional code, it's undefined.
========================================================== 7. is this javascript broken? if so, why?
var submit = document.getElementById("submit");
submit.click();
teh answerz...
Dunno. .click() didn't always exists, but it seems to work in modern browsers.
<!-- A -->
<table>
<tr>
<td class='key' style='width:33%'>
{{ dynamic_content_for(:key) }}
</td>
<td class='val'>
{{ dynamic_content_for(:val) }}
</td>
</tr>
</table>
<!-- B -->
<div class='fluid grid'>
<div class='row'>
<span class='key width33'>
{{ dynamic_content_for(:key) }}
</span>
<span class='val'>
{{ dynamic_content_for(:val) }}
</span>
</div>
</div>
teh answerz...
Probably A. It's really a question of semantics, is the data tabular (which would be a good guess for key/value pairs)? Then it goes in a table. Is it something else? Then it goes in a div.
# A
if var == 42
"..."
end
# B
if 42 == var
"..."
end
teh answerz...
There's a school of thought that putting the unmodifiable value on the LHS prevents acidental assignment. Personally, I don't think it reads well.
@response =
http_request(url, :method => method, :params => params, :headers => headers)
if @response.ok?
ship_it!
else
debugger!
end
teh answerz...
Inspect @response for obvious errors. Hit the URL with curl to see what the real world response is.
# A
if foo.bar.baz
'...'
end
# B
if foo
if foo.bar
if foo.bar.baz
'...'
end
end
end
teh answerz...
Technically, B is better because it doesn't blow up if foo or bar is nil. However, B is ugly as sin. Null Object Pattern, anyone?
========================================================== 12. is this javascript broken? if so, why?
ajax(url)({
'method' : 'POST',
'params' : params,
'headers' : headers
});
teh answerz...
Assuming that ajax() returns function, then this is perfectly valid code.
'#FFE'
teh answerz...
"#FFE" -> "#FFFFEE" which is a yellowy off white.
0b101010
teh answerz...
ruby -e 'puts 0b101010'
========================================================== 15. describe an algorithm to detect the 'edges' in these pixels
0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 1 0 0 1 1 1 0 0 0 0 0 0 0 0
teh answerz...
This answer intentionally left blank.
$color = [@X, @R, @G, @B].as_hex
teh answerz...
Transparency.
========================================================== 16. what are the advantages of static linking?
export HINT=$LD_RUN_PATH
teh answerz...
The libraries being linked at run time can be changed at any time either by playing with the load path or by out right replacing the files.
var uploader = new qq.FileUploader(options);
var image_ids = [];
uploader._uploadFileList = function(){
image_ids = [];
var args = Array.prototype.slice.call(arguments);
var result = qq.FileUploaderBasic.prototype._uploadFileList.apply(uploader, args);
return(result);
};
teh answerz...
It overrides uploader._uploadFileList to pre-process the arguments before calling the original function. Think alias_method_chain.
jQuery('.help').find('a').attr('tabindex', '-1');
teh answerz...
Finds all elements with class "help" then find "a" elements in those results and sets the 'tabindex' attribute to -1 (which make the element not be "keyboard focusable").
~ > run_api_tests_locally
"FAIL: you must access teh API from a box on EC2 with IP 1.2.3.4!"
teh answerz...
Assuming that a) I'm suppost to be hitting the live API and b) I have access to that EC2 instance, I would set up an SSH tunnel from the locally machine to EC2 and talk to the API through it.
If these are suppose to be purely local tests, I'd mock out the API with webmock, fakeweb, vcr, etc.
// A
User.prototype.first_name = function(){
return this.name.split(/\s+/)[0];
};
// B
User.prototype.first_name = function(){
return User.first_name_for(this.name);
};
User.first_name_for = function(name){
return name.split(/\s+/)[0];
};
teh answerz...
I guess you could argue that B make it easier to override.