These labs dogfood
All slides and lab materials using open web technologies.
- Explore both content and implementation!
We are using inspire.js for these slides!
All slides and lab materials using open web technologies.
The Web’s primary innovation
https://
dsc106.com
/labs/1/hello.html
The file:
protocol
file://
/Users/giorgianicolaou/Documents/dsc106/labs/1/hello.html
Structure
<h1>Hello world!</h1>
<p>This is my
<em>first</em> web page! 🤯
</p>
Presentation
body {
font: 100%/1.5 system-ui;
}
h1 {
color: deeppink;
font-size: 300%;
}
Behavior
document.onclick = event => {
alert(`You clicked at
${event.x}, ${event.y}`);
};
It tries to fix your mistakes
<em>Emphasized
<strong>Both
</em> Important</strong>
<em>Emphasized
<strong>Both</strong>
</em>
<strong>Important</strong>
It ignores your mistakes
h1 {
color: slategray;
foobar: yolo;
}
h1 {
color: slategray;
}
It stops at the first mistake
alert("Hello world!"
Uncaught SyntaxError: missing ) after argument list
📂 File Structure:
index.html
main.js
modules/
canvas.js
square.js
📌 canvas.js — Handles Canvas Setup
function create(parent, width, height, id) {
let wrapper = document.createElement("div");
wrapper.id = id;
let canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
wrapper.appendChild(canvas);
parent.appendChild(wrapper);
return { context: canvas.getContext("2d"), id };
}
function createReportList(wrapperId) {
let list = document.createElement("ul");
document.getElementById(wrapperId).appendChild(list);
return list.id;
}
📌 square.js — Handles Square Operations
const name = "square";
function draw(context, x, y, size, color) {
context.fillStyle = color;
context.fillRect(x, y, size, size);
return { size, x, y, color };
}
function reportArea(listId, length) {
let list = document.getElementById(listId);
let item = document.createElement("li");
item.textContent = `Square area: ${length * length}`;
list.appendChild(item);
}
function reportPerimeter(listId, length) {
let list = document.getElementById(listId);
let item = document.createElement("li");
item.textContent = `Square perimeter: ${length * 4}`;
list.appendChild(item);
}
📌 main.js — Bringing It All Together
import { create, createReportList } from "./modules/canvas.js";
import { draw, reportArea, reportPerimeter } from "./modules/square.js";
const parent = document.body;
const { context, id } = create(parent, 400, 400, "canvas-wrapper");
const listId = createReportList(id);
let square = draw(context, 50, 50, 100, "blue");
reportArea(listId, square.size);
reportPerimeter(listId, square.size);
📌 Function Definitions
// JavaScript
function greet(name) {
return "Hello, " + name;
}
# Python
def greet(name):
return "Hello, " + name
📌 For Loops
// JavaScript
for (let i = 0; i < 5; i++) {
console.log(i);
}
# Python
for i in range(5):
print(i)
📌 Conditionals
// JavaScript
if (x > 10) {
console.log("Large");
} else {
console.log("Small");
}
# Python
if x > 10:
print("Large")
else:
print("Small")
📌 Importing Modules
// JavaScript
import fs from 'fs';
# Python
import os
📌 Variable Declarations
// JavaScript
let count = 5;
const name = "Alice";
# Python
count = 5
name = "Alice"
📌 Lists vs Arrays
// JavaScript
let fruits = ["apple", "banana", "cherry"];
fruits.push("date");
# Python
fruits = ["apple", "banana", "cherry"]
fruits.append("date")
a:hover {
background: gold;
}
<a href="#">Come here</a>
<a href="#"
onmouseover="this.style.background = 'gold';"
>Come here</a>
<a href="#"
onmouseover="this.style.background = 'gold';"
onmouseout="this.style.background = '';"
>Come here</a>
<input type="number" id="input_a"> +
<input type="number" id="input_b"> =
<input type="number" id="input_c" disabled>
let a = 1, b = 2, c;
input_a.value = a;
input_b.value = b;
input_a.addEventListener("input", e => {
a = Number(input_a.value);
updateC();
});
input_b.addEventListener("input", e => {
b = Number(input_b.value);
updateC();
});
function updateC() {
c = a + b;
input_c.value = c;
}
updateC();
let a = 1, b = 2, c;
input_a.value = a;
input_b.value = b;
function render() {
a = Number(input_a.value);
b = Number(input_b.value);
c = a + b;
input_c.value = c;
}
render();
input_a.addEventListener("input", render);
input_b.addEventListener("input", render);
document.body.addEventListener("input", render);
{
"id": "cuisine-of-nepal-san-francisco",
"name": "Cuisine of Nepal",
"image_url": ...,
"is_closed": false,
"url": ...,
"review_count": 303,
"categories": [...],
"rating": 4.5,
"coordinates": {...},
"transactions": [
"delivery",
"pickup",
"restaurant_reservation"
],
"price": "$$",
"location": {...},
"phone": "+14156472222",
"display_phone": "(415) 647-2222",
"distance": 2502.5961202999997,
"attributes": {
"gender_neutral_restrooms": true
},
"data": {
"reservation_openings": [
"13:30",
"13:45",
"14:00"
]
}
},
let response = await fetch("https://api.github.com/users/giorgianicolaou");
let json = await response.json();
// Do stuff with json
fetch("https://api.github.com/users/giorgianicolaou")
.then(response => response.json())
.then(json => {
// Do stuff with json
});
async function fetchUserData() {
try {
const response = await fetch("https://api.github.com/users/giorgianicolaou");
const user = await response.json();
console.log("User data:", user);
const reposResponse = await fetch(user.repos_url);
const repos = await reposResponse.json();
console.log("Repositories:", repos);
} catch (error) {
console.error("Error:", error);
}
}
fetchUserData();