Racket Quiz
The first half of the lab will be T1.
Background
Getting started
- make a directory
labs/L08inside your~/cs2613git repository - All of your work from today should be committed in that directory (and pushed before you leave).
Questions
- Time
- 5 Minutes
- Activity
- Group discussion / Announcments
- Questions about Quiz 1?
- Questions about Assignment 2?
- Questions about Programming Contest
Running JavaScript
- Time
- 15 minutes
- Activity
- Demo
In a file
- Create a new file
~/cs2613/labs/L08/hello-world.js - Add some JavaScript code, save
console.log("Hello world");
Running a script
open a terminal, find the right directory
$ node hello-world.js
commit your file
In a Browser
- Note
- Examples involving "prompt" and "alert" will only work in a browser.
Start Firefox
Open the "Web Console" with Ctrl-Shift-K (or navigate via the "hamburger menu" to "Web Developer -> Web Console" ).
Select the tab "Console"
Copy-paste the code in.
In the REPL
Start up a node repl
$ node
Load your code into the repl
> .load hello-world.jsEvaluate some expressions
> 1 + 1 > "Hello " + "world!"
JavaScript equality and type coercion
- Time
- 10 Minutes
- Activity
- Demo
From Eloquent JavaScript Chapter 1, and the WAT talk by Gary Bernhardt, we know that type coercion is an important part of evaluating JS expressions. This is sometimes useful
> 42 + 'is a big number'
But just as often a source of errors and confusion.
> "" + 1
> x=""
> x++
One operator where type coercion can be particularly surprising is the
standard equality test ==. Not only does type coercion apply:
> "" == 0
> false == 0
> "" == 0
but special rules apply to "undefined" and "null"
> false == undefined
> undefined == null
> undefined == undefined
even though they would normally be considered falsy (considered false in boolean contexts).
> if (undefined) { console.log("truthy") } else { console.log("falsey") }
NaN is another falsy value not == to the other falsy values, not even itself:
> NaN == undefined
> NaN == NaN
To avoid this twisty maze of "helpful" type coercion, you can use the "strict equality" checker ===
> "" === 0
> false === 0
> "" === 0
> false === undefined
> undefined === null
> undefined === undefined
Javascript functions
- Time
- 20 minutes
- Activity
- Individual
Reference for this section is JavaScript functions Like Racket, JavaScript has two different ways of defining functions. The first way is by assigning an anonymous function to a variable.
(define square (lambda (x) (* x x)))
let square = x => x*x;
let square2 = function (x) {return x*x};
- Some questions to think about:
How is assignment (
=) in javascript different from binding (let,define) in Racket?returnis mandatory. What does it mean?what is the difference between the two kinds of anonymous functions in JavaScript? Hint: the first example is an arrow function.
The more compact way of defining functions in both Racket and
JavaScript combines the binding and creation of a function/lambda
(define (square x) (* x x))
function square(x) { return x*x }
- Start a new file
~/cs2613/labs/L08/loop-arith.js, and fill in the following definition formultusingplusandfor.
const plus = (a,b) => {
for (let i=0; i < a; i++){
b++;
}
return b;
}
const mult = function (a,b) {
sum=0;
return sum;
}
- Test your file manually, using e.g. the
nodeREPL.