29.07.2016 Views

front-end-developer_1_

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Front-End-Developer - Level 1<br />

#drinks, #under-21 {<br />

}<br />

display: none;<br />

scripts.js<br />

$(document).ready(function() {<br />

var age = parseInt(prompt("How old are you?"));<br />

if (age >= 21) {<br />

$('#drinks').show();<br />

} else {<br />

$('#under-21').show();<br />

}<br />

});<br />

Since prompt() returns a string, we have to change it into a number using the parseInt()<br />

function. (The Int bit is short for integer, which means it's a whole number. If we wanted to<br />

convert something into a number with a decimal, we'd use parseFloat - floating point<br />

numbers are numbers with decimals.)<br />

The new part of the JavaScript is called an if...else statement, or a branch. If the<br />

condition in the parentheses is true, then the first statement is run. If not, the statement<br />

following the else keyword is run.<br />

The curly braces after the if statement don't <strong>end</strong> with a semicolon. This is because an<br />

if statement isn't a statement itself; rather, it's a collection of statements. Or something<br />

like that. Just know that it's a rule: no semicolons after the curly braces for if statements.<br />

Also, once again, note the indentation: everything that inside the if and else gets<br />

indented in two spaces.<br />

You can even make the logic a little more complex:<br />

scripts.js<br />

Branching<br />

120

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!