Published unlisted
Edited
Dec 17, 2020
Insert cell
Insert cell
Insert cell
Insert cell
{
// Function to evaluate a variable based on a string of its name
const checkValue = variable_of_interest => {
// Check if the variable is defined
if (eval("typeof " + variable_of_interest) === "undefined") {
return `${variable_of_interest} isn't defined`;
} else {
// Check if it matches the value in the `answers` object
return eval(variable_of_interest) === answers[variable_of_interest]
? `${variable_of_interest} is correctly assigned!`
: `${variable_of_interest} is not correctly assigned!`;
}
};

// Create some values to check
const value_1 = 1;
const value_2 = 100;
return [checkValue("value_1"), checkValue("value_2"), checkValue("value_3")];
}
Insert cell
Insert cell
// Function to evaluate a variable based on a string of its name
checkValue = variable_of_interest => {
// Check if the variable is defined
if (eval("typeof " + variable_of_interest) === "undefined") {
return `${variable_of_interest} isn't defined`;
} else {
// Check if it matches the value in the `answers` object
return eval(variable_of_interest) === answers[variable_of_interest]
? `${variable_of_interest} is correctly assigned!`
: `${variable_of_interest} is not correctly assigned!`;
}
}
Insert cell
// Create a cell holding a value
value_1 = 100
Insert cell
// This unfortunately doesn't work for Observable cell values
checkValue("value_1")
Insert cell
// Unclear why this returns undefined
eval("typeof value_1") === "undefined"
Insert cell
typeof value_1
Insert cell
// Also doesn't work in a JavaScript block
{
return String(eval("typeof value_1"));
}
Insert cell
// Oddly, it looks like it recognizes the type *if the cell references the variable*
{
return String(value_1) + " " + String(eval("typeof value_1"));
}
Insert cell
Insert cell
// Sample answers
answers = ({ value_1: 1, value_2: 2, value_3: 3 })
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