Public
Edited
Nov 11, 2024
Importers
1 star
Insert cell
Insert cell
convertTemp(1.1870614111044087, { input: "c", output: "f", degree: false })
Insert cell
// Convert Celsius to Fahrenheight, in degrees
convertTemp(0, {
input: "c",
output: "f",
degree: true
})
Insert cell
// Convert Celsius to Fahrenheight, in increment
convertTemp(2, {
input: "c",
output: "f",
degree: false
})
Insert cell
// Convert Celsius to Fahrenheight, in increment
convertTemp(1.5, {
input: "c",
output: "f",
degree: false
})
Insert cell
// Convert Fahrenheight to Celsius, in degrees
convertTemp(32, {
input: "f",
output: "c",
degree: true
})
Insert cell
// Convert Fahrenheight to Celsius, in increment
convertTemp(2.7, {
input: "f",
output: "c",
degree: false
})
Insert cell
// Convert Celsius to Kelvin, in degrees
convertTemp(0, {
input: "c",
output: "k",
degree: true
})
Insert cell
// Convert Kelvin to Celsius, in degrees
convertTemp(273.15, {
input: "k",
output: "c",
degree: true
})
Insert cell
// Convert Fahrenheight to Kelvin, in degrees
convertTemp(0, {
input: "f",
output: "k",
degree: true
})
Insert cell
// Convert Kelvin to Fahrenheiht, in degrees
convertTemp(300, {
input: "k",
output: "f",
degree: true
})
Insert cell
convertTemp(null)
Insert cell
convertTemp(undefined)
Insert cell
convertTemp(Infinity)
Insert cell
convertTemp("foo")
Insert cell
convertTemp(["foo"])
Insert cell
convertTemp({"foo": "bar"})
Insert cell
convertTemp(0)
Insert cell
// Works with Celsius, Fahrenheight and Kelvin
// Returns degree or increment
function convertTemp(value, {
input = "c",
output = "f",
degree = true
} = {}) {
if (value === null || isNaN(value) || !isFinite(value) || (value !== 0 && !value)) return null;
const io = input + "-" + output;

switch (io) {
case "c-c":
case "f-f":
case "k-k":
return value;
case "c-f":
return value * 9 / 5 + (degree ? 32 : 0);
case "c-k":
return value + (degree ? 273.15 : 0);
case "f-c":
return (value + (degree ? -32 : 0)) * 5 / 9;
case "f-k":
return (value + (degree ? -32 : 0)) * 5 / 9 + (degree ? 273.15 : 0);
case "k-c":
return degree ? value - 273.15 : value;
case "k-f":
return (degree ? value - 273.15 : value) * 9 / 5 + (degree ? 32 : 0);
}
}
Insert cell
import { getCurrentPinnedName } from '@bryangingechen/version-pinning-for-notebooks@469'
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