#functions - we made it!!! i love you!!!!!
##3.4.1 Functions
###3.4.2 Scope review questions:
-
will the
console.log()
afterfun1()
be different from theconsole.log()
afterfun2()
? (paste the code between script tags and run it on your browser!)://example 1 console.log("example 1:"); var myfavsong = "let it go from the movie Frozen"; console.log("myfavsong in global scope: "+myfavsong); function fun1() { myfavsong = "call me maybe"; console.log("myfavsong in fun1 local scope: "+myfavsong); } fun1(); console.log("myfavsong in global scope: "+myfavsong); //example 2 console.log("\n"); console.log("example 1:"); var myfavsong = "let it go from the movie Frozen"; console.log("myfavsong in global scope: "+myfavsong); function fun2(myfavsong) { myfavsong = "kind of blue"; console.log("myfavsong in fun2 local scope: "+myfavsong); } fun2(myfavsong); console.log("myfavsong in global scope: "+myfavsong);
-
what's an IIFE? why is it useful?
name
is a reserved word in the chrome console (i.e. chrome is already using name
as a variable name). If you use the variable name name
in place of username
in the 3.4.1 exercise (see link to solution below), it casts to a string. For example, if the user clicks cancel in response to the first prompt, the null
returned by prompt()
will cast to the string "null"
and fail the while loop conditional. moral of the story? variables should be as specific as possible! An IIFY should also fix this issue - test it out!.
##more practice
##solutions to Section 3.4 Exercises
##solution to this mornings hello world exercise:
<!DOCTYPE html>
<html>
<head>
<title>hello world js</title>
<meta charset="utf8">
</head>
<body>
<script type="text/javascript">
"use strict"
/*
* How many times a lucky number is repeated for every 100 customers?
*/
var luckyNumber;
var i = 1;
var count0=0;
var count1=0;
var count2=0;
var count3=0;
var count4=0;
var count5=0;
while (i < 100) {
luckyNumber = Math.floor(Math.random()* 6);
switch(luckyNumber){
case 0:
count0++
break;
case 1:
count1++
break;
case 2:
count2++
break;
case 3:
count3++
break;
case 4:
count4++
break;
case 5:
count5++
break;
}
i++;
}
console.log("0 appeared "+count0+" times");
console.log("1 appeared "+count1+" times");
console.log("2 appeared "+count2+" times");
console.log("3 appeared "+count3+" times");
console.log("4 appeared "+count4+" times");
console.log("5 appeared "+count5+" times");
// The output should be something like
// 0 appeared 10 times
// 1 appeared 20 times
// 2 appeared 15 times
// 3 appeared 35 times
// 4 appeared 4 times
// 5 appeared 16 times
// The total should be 100
</script>
</body>
</html>
##party time! (solution to fibonacci loop challenge)
<!DOCTYPE html>
<html>
<head>
<title>aah loops!</title>
<meta charset="utf8">
</head>
<body>
<script type="text/javascript">
"use strict"
var fibnum = 30;
var n_1=0;
var n_2=0;
var n=0;
for (var i = 0; i <= fibnum; i++) {
n= n_1 + n_2;
console.log(n);
if (n==0) {
n+=1;
console.log(n);
}
n_2 = n_1;
n_1 = n;
};
</script>
</body>
</html>