Calvin (Deutschbein)
6 March 2023
<!DOCTYPE html>
<html>
<body>
<p id="showEle">1</p>
<button onclick="update()">Click me</button>
<script>
let val = 1 ;
const showVar = document.getElementById("showEle") ;
function update()
{
val = 2 * val ;
showVar.innerText = val.toString() ;
}
</script>
</body>
</html>
Consider the following HTML excerpt.
<p id="showEle">1</p>
In the overall sample code shown, what was the usefulness of the the id
attribute?
Consider the following HTML excerpt.
<button onclick="update()">Click me</button>
In the overall sample code shown, what was the usefulness of the the onclick
attribute?
<!DOCTYPE html>
<html>
<body>
<p id="showEle">1</p>
<button onclick="update()">Click me</button>
<script>
let val = 1 ;
const showVar = document.getElementById("showEle") ;
function update()
{
val = 2 * val ;
showVar.innerText = val.toString() ;
}
</script>
</body>
</html>
Consider the following JavaScript excerpt from within the HTML document.
let val = 1 ;
This line is written outside of a function code block. This line:
Consider the following JavaScript excerpt from within the HTML document.
const showVar = document.getElementById("showEle") ;
This line is written outside of a function code block. This line:
Consider the following JavaScript function from within the HTML document.
function update()
{
val = 2 * val ;
showVar.innerText = val.toString() ;
}
Consider the first line function update()
Consider the following JavaScript function from within the HTML document.
function update()
{
val = 2 * val ;
showVar.innerText = val.toString() ;
}
Consider the line val = 2 * val ;
let
Consider the following JavaScript function from within the HTML document.
function update()
{
val = 2 * val ;
showVar.innerText = val.toString() ;
}
Consider the excerpt showVar.innerText
Consider the following JavaScript function from within the HTML document.
function update()
{
val = 2 * val ;
showVar.innerText = val.toString() ;
}
Consider the excerpt val.toString()
HTML/CSS styles pages.
JavaScript allows changes to HTML/CSS without reloads.
HTML/CSS/JavaScript have firm separation.
Continue with the <script>
element.
<script>alert('Hello, world!');</script>
We understand the following:
Omelas is an attempted proof-by-contradiction* for the necessity of equality under utilitarianism. It proceeds roughly as follows.
*I don't remember how proofs work.
My key insights are as follows.
Loss aversion was first described by name in 1979, 6 years after Omelas was written.
I personally read LeGuin as anti-Marxist, but attribute this to the medium and not to the philosophy.
Loss aversion: the tendency to prefer avoiding losses to acquiring equivalent gains.
Imagine: You have 0 USD and a clean bill of health. Consider:
In both scenarios, you have net 0 USD in credits and debits, but one may feel preferable to you.
We can formulate this in other ways. Consider the following:
Do you accept?
Some economist claim* that many people will accept tradeoffs under more favorable conditions.
Do you accept?
I claim:
Scientific socialists such as Angela Davis and Karl Marx view social and political developments as being largely determined by economic conditions.
From there, all that remains is to define a function utility
that accepts as input a level of material and produces as output a numer of utils.
We may begin by naively supposing that utility is a linear function of material.
function utility(material)
{
return material ;
}
While trivial to plot we do so, so that as our function develops we may inspect it visually.
We introduce the canvas
HTML element.
<body onload="draw();">
<canvas id="canvas" width="400" height="400"></canvas>
</body>
Just as paragraphs allows us to change content with innerText, canvas elements allow us to draw rectangles.
function draw()
{
const plot = document.getElementById("plot") ;
const ctx = plot.getContext("2d");
ctx.fillRect(50, 50, 50, 50) ;
}
Rects default to black, background to transparent.
I'll use a CSS style to put the created .png files on a high contrast white background and to scale them to slide size.
Let's make a plot by adding axes in not-quite-black.
const size = 400 ;
const half = size / 2 ;
ctx.fillStyle = "dimgrey";
for (let i = 0 ; i < size ; i++ )
{
ctx.fillRect(i, half, 1, 1) ;
ctx.fillRect(half, i, 1, 1) ;
}
There - now we can tell where things are at.
Then we plot our utility function.
for (let i = 0 ; i < size ; i++ )
{
ctx.fillRect(i, utility(i), 1, 1) ;
}
Ok we have a plot - let's clean this up a bit.
Let's increase line thickness to 2 pixels and transpose the plot so all positive values to be up and to the right.
for (let i = 0 ; i < size ; i++ )
{
let j = size - utility(i)
ctx.fillRect(i, j, 2, 2) ;
}
This looks nice enough to me, but we have no scale.
Let's scale so this plot runs for (-2,2) and add we'll tick marks (not shown) .
const range = 2 ;
const full = range * 2 ;
let x, y, j ;
// tick marks - every size / range * 2 - snipped
// scale pixels to (-2,2)
for (let i = 0 ; i < size ; i++ )
{
x = (i / size) * full - range ;
y = utility(x) ;
j = ((range - y) / full) * size ;
ctx.fillRect(i, j, 2, 2) ;
}
There we go! Now we iterate on utility.
We may begin by naively supposing that utility is a linear function of material.
function utility(material)
{
return material ;
}
In general, people prefer to avoid equivalent losses.
function f(x)
{
if (x < 0)
{
return 2 * x ;
}
return x ;
}
We now use f(x) for simplicity.
In general, aversion to losses increases as losses grow larger.
function f(x)
{
if (x > 0)
{
return x ;
}
return Math.log(x + 1);
}
We now use f(x) for simplicity.
In general, gains experience diminishing marginal returns.
function f(x)
{
return Math.log(x + 1) ;
}
At the status quo, adding or removing a dollar is roughly equivalent.
function f(x)
{
return Math.log(x + 1) ;
}
We show this by showing the instantaneous slope at zero is equal to one with a red tangent line.
We get discontinuity when slope exceeds one as we were displaying at most one pixel in each vertical column of pixels.
const range = 2 ;
const full = range * 2 ;
let x, y, j ;
// tick marks - every size / range * 2 - snipped
// scale pixels to (-2,2)
for (let i = 0 ; i < size ; i++ )
{
x = (i / size) * full - range ;
y = f(x) ;
j = ((range - y) / full) * size ;
ctx.fillRect(i, j, 2, 2) ;
}
We will fix this by scanning in both i and j
First, we precompute k values. These are the old j values, and will be compared to j values.
const ks = [] ;
// we do this off by one to help with bounds
for (let i = -1 ; i < size + 1 ; i++ )
{
x = (i / size) * full - range ;
y = f(x) ;
k = ((range - y) / full) * size ;
ks.push(k)
}
We compute what the old j value would be outside of the canvas as well - we'll come back to that.
Now we iterate over each pixel, and check to see if a k is within 1 pixel of j.
for (let i = 0 ; i < size ; i++ )
{
for (let j = 0 ; j < size ; j++ )
{
// ensure shading when slope < 1
// we fill at least one pixel in every row
if (Math.abs(j - ks[i + 1]) < 1)
// Remember k's are off by one.
{
ctx.fillRect(i, j, 1, 1) ;
}
This means we will always shade at least one pixel per row - if slope is less than one, j and k are within 1 pixel more than once.
Now we iterate over each pixel, and check to see if a k is within 1 pixel of j.
// ensure shading when slope > 1
// fill at least one pixel in every column.
closer = Math.min(Math.abs(j - ks[i]),Math.abs(j - ks[i+2]))
if (Math.abs(j - ks[i+1]) < closer)
{
ctx.fillRect(i, j, 2, 2) ; // line was 2 pixels
}
This means we will always shade at least one pixel per column - if slope is greater than one, we simply keep adding rectangles until our vertical position is closer to another horizontal position than the current horizontal position.
That's better!
Presumably there's like libraries to do this or whatever.
Utilitarianism demands equality when considering loss aversion and materialism.
function f(x)
{
return Math.log(x + 1) ;
}
Utilitarianism demands equality when considering loss aversion and materialism.
Utilitarianism demands equality when considering loss aversion and materialism.
∴ Utilitarianism demands equality when considering loss aversion and materialism.∎
https://github.com/cd-public/eths23/blob/main/html/utilator.html