Public
Edited
Sep 5, 2024
Insert cell
Insert cell
Insert cell
paragraph = "Since its founding in 1831, NYU has been an innovator in higher education, reaching out to an emerging middle class, embracing an urban identity and professional focus, and promoting a global vision that informs its 20 schools and colleges."
Insert cell
// Use .length to find length
len = paragraph.length
Insert cell
Insert cell
// use .split to split the paragraph by spaces
words = paragraph.split(' ')
Insert cell
Insert cell
// words.length to find number of words
num_words = words.length
Insert cell
Insert cell
// Use second index in words list
second_word = words[1]
Insert cell
Insert cell
// Use sets to find unique values
unique_words = new Set(words);
Insert cell
// Use .size to find length of unique_words set
num_unique = unique_words.size
Insert cell
Insert cell
prices = ({
milk: 3.45,
crackers: 4.54,
pepsi: 1.00,
chips: 2.98,
})
Insert cell
// Can use dot or brackets for access
milk_val = prices.milk
Insert cell
Insert cell
food = "chips"
Insert cell
// Can use dot or brackets for access
food_val = prices[food]
Insert cell
Insert cell
[
["milk", 3.45],
["crackers", 4.54],
["pepsi", 1.00],
["chips", 2.98]
]
Insert cell
// Use .entries to convert to object
kvps = Object.entries(prices)
Insert cell
Insert cell
// Use from key value pairs to convert to object
Object.fromEntries(kvps)
Insert cell
Insert cell
// create new map using constructor - pass in array
items = new Map(kvps)
Insert cell
Insert cell
// Use .get to retrieve
milk_value = items.get("milk")
Insert cell
Insert cell
snack = "chips"
Insert cell
// Use .get to retrieve
snack_value = items.get(snack)
Insert cell
Insert cell
// Use .from in order to get array from map
// [...nameToAge.keys()] - use state alternatively
Array.from(items.keys())
Insert cell
Insert cell
// Use .from again to cast
// Array.from(nameToAge)
// Can be used to create object from map - Object.fromEntries(Array.from(nameToAge))
Array.from(items)
Insert cell
Insert cell
// answer
Array.from(items, pair => ({ item: pair[0], cost: pair[1] }))
Insert cell
Insert cell
function triangleArea(base, height) {
return base * height / 2;
}
Insert cell
triangleArea(10, 20)
Insert cell
// arrow function
//
area_arrow_func = (base, height) => `The area of a triangle with base and height ${base} and ${height} is: ${0.5*base*height}`
Insert cell
area_arrow_func(10,20)
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