Last Time We learned about three types of popups. All three are created by functions; you can pass all of them a string as an argument which gets displayed on the popup.
alertThis is a popup with a message. It has no return valueconfirmThis popup features two buttons: cancel and OK. If OK is selected, it has a return value oftrue; otherwise, the return value isfalse.promptThis popup features a text box the user can type in. The string that is typed is returned. However, if the user hits cancel, the graveyard objectnullis returned.
Conditional Logic: Selection
Simple if A simple
if looks like this.
if(predicate)
{
//block o'code
}
Here are its features.
- The predicate is some expression that evaluates to a boolean.
- The predicate must be in parentheses.
- If the predicate evaluates to
truethe block of code executes. - If the predicate evaluates to
false, the code is skipped.
We use a simple if to create an absolute value
function.
function foo(x)
{
//if predicate is false, block is skipped.
if( x < 0)
{
x = -x;
}
return x
}
if with else
Here we have an if-else progression.
if(predicate)
{
//true_code
}
else
{
//false_code
}
Here are its features.
- The predicate is some expression that evaluates to a boolean.
- The predicate must be in parentheses.
- If the predicate evaluates to
truethetrue_codeexecutes. - If the predicate evaluates to
false,false_codeexecutes. - Exactly one of the two blocks exectues.
- No non-conditonal code can go beteen the
ifand theelse.
else if
if(predicate)
{
//code0
}
else if(predicate1)
{
//code1
}
else if(predicate2)
{
//code2
}
else if(predicate3)
{
//code3
}
.
.
.
else if(predicateN)
{
//codeN
}
else
{
//default_code
}
Here are its features.
- The predicates must be enclosed in parentheses.
- Non-conditional code cannot go between elements in the progression.
- JS keeps trying until one of the predicates is true. once that happens, the corresponding block executes and you are out of the progression.
- The
elseelement is entirely optional. If none of the other elements execute, it executes. - Only one of the blocks exectues. Exaclty one will
execute if there is an
elseelement.
Here we make a signum function.
function sgn(x)
{
if(x > 0)
{
return 1;
}
else if(x < 0)
{
return -1;
}
else
{
return 0;
}
}
Here we compute a letter grade.
function compute_grade(score)
{
if( score >= 90)
{
return "A";
}
else if(score >= 80)
{
return "B";
}
else if(score >= 70)
{
return "C";
}
else if(score >= 60)
{
return "D";
}
else
{
return "F";
}
}
Puzzler Implement this.
$$ f(x) = \begin{cases} -x, &\text{ if}\ x\leq 0\\ x^2, &\text{ if}\ 0\lt x\leq 1\\ 2x-1, &\text{ if}\ x\geq 1\\ \end{cases} $$Going Overboard Suppose we want to
create a function monthName that accepts an integer
1-12 and which returns "Jan" for 1, "Feb" for 2, etc. We
could do this with a conditional contstruct. It's an easy exercise
to create it. OR we could do this.
function monthName(n)
{
// 1 for Jan, 2 for feb, etc.
names = ["", "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
return names[n]
}
console.log(monthName(9));
The Inside Dope on addEventListener
The addEventListener method can be called on any
page element. This method requires two arguments: the first is
an event type, and the second is a function to be called when
the event of the specified type occurs in the element.
So far, the way we get page elements is via document.getElementById.
We met these event types.
loadThis event is broadcast by the browser when a page loads.clickThis event is broadcast when an element on a page is clicked by the user.
Here are some more.
mouseoverThis event is broadcast when the mouse is over an element.mouseleaveThis event is broadcast when the mouse exits an element.mouseenterThis event is broadcast when the mouse enters an element.mousedownThis event is broadcast when the mouse is down on element.mouseupThis event is broadcast when the mouse is down on element.resizeThis event is broadcast when the page resizes. is down on element.
The The W3Schools DOM Event Reference has the whole enchilada.
<!doctype html>
<!--Author: Morrison-->
<html>
<head>
<title>eventdemo</title>
<link rel="stylesheet" href="eventdemo.css"/>
<script src="eventdemo.js">
</script>
</head>
<body>
<h1 id="headline">Event Demonstration</h1>
<p>Can you make the list items bold when then they are moused
over and unbold otherwise?</p>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
<li>three</li>
</ul>
</body>
</html>
/*Author: Morrison*/
h1, h2, .display
{
text-align:center;
}
/*Author: Morrison*/
window.addEventListener("load", main);
function makeYellow()
{
let headline = document.getElementById("headline");
headline.style.backgroundColor = "yellow";
// background-color -> backgroundColor
}
function unmakeYellow()
{
let headline = document.getElementById("headline");
headline.style.backgroundColor = "white";
}
function makeBold()
{
this.style.fontWeight = "bold";
}
function unmakeBold()
{
this.style.fontWeight = "normal";
}
function main()
{
let headline = document.getElementById("headline");
headline.addEventListener("mouseenter", makeYellow);
headline.addEventListener("mouseleave", unmakeYellow);
//code smell: repetition!
/*li1 = document.getElementById("li1");
li2 = document.getElementById("li2");
li3 = document.getElementById("li3");
li1.addEventListener("mouseover", makeBold);
li2.addEventListener("mouseover", makeBold);
li3.addEventListener("mouseover", makeBold);
li1.addEventListener("mouseleave", unmakeBold);
li2.addEventListener("mouseleave", unmakeBold);
li3.addEventListener("mouseleave", unmakeBold);*/
let listItems = document.getElementsByTagName("li");
//this allows us to avoid the duplication we
//saw above and, if you add items to the list,
//the event handling code is automatically
//added.
for(let k = 0; k < listItems.length; k++)
{
listItems[k].addEventListener("mouseover", makeBold);
listItems[k].addEventListener("mouseleave", unmakeBold);
}
}
The The W3Schools DOM Event Reference has the whole enchilada.
Replacing Text and HTML in an Element
Sometimes you will want to alter text or HTML inside of a page's element. Here is how to replace text.
let element = document.getElementById("someId");
element.innerText = "new text";
Here is how to replace HTML.
let element = document.getElementById("someId");
element.innerHTML = "new HTML";