The last part of the seven-item front end exam by HackerRank consists of five multiple choice questions with topics on JavaScript, CSS and overall DOM manipulation.
Table of Contents
- Table of Contents
- Missing Code for Hover Color Green to Red
- Ask to Download
- Start at Page Element
- Field Creation
- Which is the right answer to the following?
- References
Missing Code for Hover Color Green to Red
Problem
Consider the files file.html, file.css and file.js.
<a href="#">Link</a>
<button>Click</button>
body {
padding: 50px;
font-size: 24px;
}
a { color: blue; }
a:hover { color: green; }
function injectStyles(magic) {
var div = $("<div/>", {
html: '­<style>' + __________ + '</style>'
}).appendTo("body");
}
The following jQuery should change the link hover color to red when the button is pressed.
$("button").on("click", function() {
injectStyles('a:hover { color: red; }');
})
Pick ONE option:
magic.getContent()
magic.parent().getContent()
magic
magic.getParent().getContent()
Solution
Item # 3 magic
completes the injectStyles()
function:
function injectStyles(magic) {
var div = $("<div/>", {
html: '­<style>' + magic + '</style>'
}).appendTo("body");
}
All the methods getContent()
, parent()
and getParent()
are not available for String
objects in JavaScript.
I did a quick search over at MDN and I only FileSystemEntry.getParent()
using the three fictitious methods.
Ask to Download
Problem
Which statement will show an image and if a user clicks on the image, the browser will ask them to download it?
<a href="/images/wallpaper.jpg" download><img src="/images/wallpaper.jpg"></a>
<a href="/images/wallpaper.jpg"><img src="/images/wallpaper.jpg" download></a>
<a href="/images/wallpaper.jpg"><img src="/images/wallpaper.jpg" onclick="download"></a>
<a href="/images/wallpaper.jpg" onClickAction=download><img src="/images/wallpaper.jpg"></a>
Solution
Item # 2 with the download
attribute attached only to the image is the answer.
The first works differently as the download
attribute is in the anchor tag. If the link is of the same-origin, then it will prompt a file download; otherwise it will just navigate to the destination URL.
The third fires an error Uncaught ReferenceError: download is not defined at HTMLImageElement.onclickgo
because onclick="downlod"
is not declared so it just navigates to the destination URL.
The fourth goes to the linked URL in the anchor tag. onClickAction
will just be ignored because it is not a standard attribute.
Start at Page Element
Problem
When a web page is loaded, the user’s cursor should be in the only text field on the page. How is this accomplished using HTML5?
<input type="text" focus=True>
<input type="text" autofocus>
<input type="text" focus="onPageLoad">
- It cannot be accomplished with only HTML5.
Pick ONE option.
Solution
This is possible with native HTML using the autofocus
attribute on input
elements so item # 2 is the answer. In Chrome, the textfield will have an orange border as the cursor is focused in it upon load.
autofocus
is applicable to input
, select
and textarea
elements.
The first and third’s focus
attributes will just be ignored because it is not a standard attribute.
Field Creation
Problem
Which code produces the following? Width and height are 100px
.
Pick ONE option.
<fieldSet style="height: 100px; width: 100px;"><Legend>Coding is Fun</Legend></fieldSet>
<fieldSet style="height: 100px; width: 100px;"><Header>Coding is Fun</Header></fieldSet>
<fieldSet style="height: 100px; width: 100px;"><Caption>Coding is Fun</Header></fieldSet>
<fieldSet style="height: 100px; width: 200px;"><Legend>Coding is Fun</Legend></fieldSet>
Solution
The first item is the correct answer.
Both items # 2 and 3 do not produce the same result because <header>
and <caption>
will not behave the same was as a <fieldset>
element.
The fourth one has valid markup but it has 200px
width which does not satisfy the requirements.
Which is the right answer to the following?
Problem
<p id="test" name="hacker">Hello</p>
Which os the following JavaScript code snippets will append “Mr. Bob” after “Hello”?
Pink ONE OR MORE options.
document.getElementById("test").innerHTML += " Mr. Bob";
document.getElementsById("hacker").innerHTML += " Mr. Bob";
document.getElementsById("test")[0].innerHTML += " Mr. Bob";
document.getElementsById("hacker")[0].innerHTML += " Mr. Bob";
Solution
The first item is the answer.
The getElementsById()
method used in the other three does not exist under the Document
object.
References
- ”<a>: The Anchor element | MDN.” MDN Web Docs, Mozilla Developer Network, 20 Dec. 2021, <
developer.mozilla.org/en-US/docs/Web/HTML/Element/a
>. - “autofocus - HTML: HyperText Markup Language | MDN.” MDN Web Docs, Mozilla Developer Network, 3 Oct. 2021, <
developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autofocus
>. - “The Field Set element - HTML: HyperText Markup Language | MDN.” MDN Web Docs, Mozilla Developer Network, 3 Oct. 2021, <
developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset
>. - Parnaby-King, Richard. “Force browser to download image files on click.” Stack Overflow, 8 July 2013, <
stackoverflow.com/questions/17527713/force-browser-to-download-image-files-on-click/17527821#17527821.
>.