Published
Edited
Jan 21, 2021
Importers
Insert cell
Insert cell
Insert cell
min = Math.min
Insert cell
max = Math.max
Insert cell
// test for long lists
max(...Array(25600).fill(0), 1) === 1
Insert cell
sum = xs => xs.reduce((acc, x) => acc + x, 0)
Insert cell
sum([1, 2, 3]) === 6
Insert cell
clamp = (value, low, high) => Math.min(high, Math.max(low, value))
Insert cell
map = (value, start0, end0, start1, end1, clampResult) => {
let result = (value - start0) * (end1 - start1) / (end0-start0) + end1;
return clampResult ? clamp(result) : result
}
Insert cell
Insert cell
random = {
return function r(n) {
return Array.isArray(n)
? n[Math.floor(r(n.length))]
: n === undefined
? Math.random()
: n * Math.random();
};
}
Insert cell
random(['a', 'b', 'c'])
Insert cell
random()
Insert cell
random(10)
Insert cell
random(10, 20)
Insert cell
md`## range`
Insert cell
function* range(a, b) {
if (b === undefined) [a, b] = [0, a];
for (let i = Math.floor(a); i < b; i++) {
yield i;
}
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more