// Create an array a
var a = [4, 7, 3, 5];
// Define the function s
function s(n) {
return n ** 2;
}
// Define object literal:
var kid = { name: "Alice", age: 11 };
// Check that typeof gives object for arrays and
// object literals. It gives function as the datatype
// of a function
document.writeln(typeof a);
document.writeln(typeof b);
document.writeln(typeof kid);
// Output: object function object
var output = "";
for(var i = 0; i < kidsArray.length; i++) {
if (kidsArray[i].age < 12) {
output += kidsArray[i].name + "\n";
}
}
Answer:
var output = "";
for(var kid of kidsArray) {
if (kid.age < 12) {
output += kid.name + "\n";
}
}
We saw earlier that the following lines round the value
of n to two digits after the decimal point:var n = 34.83728; var nRounded = Math.round(n * 100) / 100;Verify that the following line also rounds n to two digits after the decimal point:
var n = 354.48592938; var nRounded = n.toFixed(2); // Answer: 354.49
btn1.addEventListener("click", f);
Show how to use the onclick property to add the event listener
f
to the button object btn1. Answer:btn1.onclick = f;Do the same thing for a load event listener.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Button</title>
<script>
function f( ) {
var output = document.getElementById("para1");
output.innerHTML = "Button test succeeded.";
}
function init( ) {
var btn1 = document.getElementById("button1");
// btn1.addEventListener("click", f);
// or
btn1.onclick = f;
}
// window.addEventListener("load", init);
// or
window.onload = init;
</script>
</head>
<body>
<h1>Test Button</h1>
<button id="button1">Click Me to Test Button</button>
<p id="para1" />
</body>
</html>
var count = 0;
let count = 0;
// for loop 1
function testScope( ) {
for(var i = 1; i <= 5; i++) {
}
return i;
}
document.writeln(testScope( ));
// Output of for loop 1
6
// for loop 2
function testScope( ) {
for(let i = 1; i <= 5; i++) {
}
return i;
}
document.writeln(testScope( ));
// Output of for loop 2
No output. The i in the return statement is not valid
because it is outside of the loop where it is defined.
// FirstClass Example
function s(n) {
return n * n;
}
function c(n) {
return n * n * n;
}
function evaluate(f) {
return f(3);
}
document.writeln(s(5) + " ");
document.writeln(c(2) + " ");
g = s;
document.writeln(g(2) + "<br>");
document.writeln(evaluate(s) +
" " + evaluate(c) + "<br>");
document.writeln(s + "<br>");
document.writeln(c + "<br>");
// Answer: the output is
25 8
4
9 27
function s(n) { return n * n; }
function c(n) { return n * n * n; }
function(n) {
return n * n;
}
// Anonymous Example
var c = function(n) {
return n * n * n;
}
document.writeln(c(5));
function square(x) {
return x * x;
}
can be defined as an anonymous
method in arrow notation like this:var square = (x) => x * x;
var square = x => x * x;
function square(n) {
return n ** 2;
}
// Answer:
// Parentheses are not needed for argument list
// when there is only one argument.
var square = n => n ** 2;
var value = 5;
document.writeln(square(value));
// Output: 25
function add(n, m) {
return n + m
}
// Answer:
var add = (n, m) => n + m;
document.writeln(add(5, 7));
// Output: 12
function sayHello( ) {
alert("Hello!");
}
// Answer
var sayHello = ( ) => alert("Hello!");
sayHello( );
// Output: Hello!
function sayHello(name) {
alert(`Hello, ${name}!`;
}
// Answer:
var sayHello = name => alert(`Hello, ${name}!`);
var name = prompt("Enter a name.");
sayHello(name);
// Input: Billy
// Output: Hello, Billy.
function showStars( ) {
return "**********";
}
// Answer:
var showStars = ( ) => "**********";
document.writeln(showStars( ));
// Output: **********
function triangleArea(base, height) {
return base * height / 2.0;
}
// Answer:
var triangleArea = base, height) => base * height / 2.0;
document.writeln(triangleArea(3, 4));
// Output: 6
function greeter(name) {
document.writeln(`Hello, ${name}, how are you?`);
}
// Answer:
var greeter = name => {
document.writeln(`Hello, ${name}, how are you?`);
}
greeter("Alice");
// Output: Hello, Alice, how are you?
function init( ) {
alert("Hello!");
}
// Answer: the body of a function with no return
// value must be enclosed in curly braces.
var init = ( ) => {
alert("Hello!");
}
// Test it like this:
window.addEventListener("click", init)
// or, shorter
window.addEventListener("click", ( ) => alert("Hello!"));
abs cbrt ceil cos floor max min random round sin sqrt
'use strict';
var s = "abc";
var t = new String("abc");