Public
Edited
Sep 23, 2023
Insert cell
Insert cell
Insert cell
num = 42
Insert cell
pi = 3.14159
Insert cell
// This line is a JavaScript comment!

// In this notebook, you can use the checkType function to see the type of a variable
// here, we see that num has the number type
checkType(num)
Insert cell
checkType(pi)
Insert cell
str = 'hello, world!'
Insert cell
str2 = "double quotes!"
Insert cell
Insert cell
checkType(str)
Insert cell
checkType(str2)
Insert cell
// booleans can have one of two values - true or false
// you can also think of them as meaning "on" or "off", "yes" or "no", 1 or 0 in binary, etc.
bool = true
Insert cell
bool2 = false
Insert cell
checkType(bool)
Insert cell
checkType(bool2)
Insert cell
Insert cell
myString = "word"
Insert cell
expectEqual(checkType(myString), "string", "myString should be a string")
Insert cell
myBoolean = false
Insert cell
expectEqual(checkType(myBoolean), "boolean", "myBoolean should be a boolean")
Insert cell
myNumber = 890
Insert cell
expectEqual(checkType(myNumber), "number", "myNumber should be a number")
Insert cell
Insert cell
arr = [num, str, bool]
Insert cell
checkType(arr)
Insert cell
// in Observable, we need to wrap objects in parentheses ()
// in plain JavaScript, we could write this as obj = { num, str, bool, otherProperty: 'foo' }
obj = (
{ num, str, bool, otherProperty: 'foo' }
)
Insert cell
checkType(obj)
Insert cell
Insert cell
Insert cell
myArray = [1, 2]
Insert cell
myVariableName = 'xyz'
Insert cell
truth = false
Insert cell
helloWorldStr = "hello, world!"
Insert cell
Insert cell
Insert cell
1 + 1
Insert cell
Insert cell
num
Insert cell
num + 1
Insert cell
num + 1 * 2 - 3 / 4
Insert cell
Insert cell
function addOne(num) {
return num + 1
}
Insert cell
addOne(5)
Insert cell
addOne(num)
Insert cell
// functions are also a type of value in JavaScript!
checkType(addOne)
Insert cell
Insert cell
// check the addOne function above if you get stuck!

function double(num) {
return num * 2
}
Insert cell
expectEqual(double(1), 2)
Insert cell
expectEqual(double(2), 4)
Insert cell
expectEqual(double(42), 84)
Insert cell
Insert cell
function addTwoNumbers(num1, num2) {
return num1 + num2
}
Insert cell
addTwoNumbers(999, 1)
Insert cell
Insert cell
// Remember to add the names of the arguments between the parentheses!
// You can name your arguments with any name you like, using the same rules as for variables above.
// For example: function multiply(num1, num2)
// Or: function multiply(a, b)

function multiply(a, b) {
return a * b
}
Insert cell
expectEqual(multiply(2, 5), 10)
Insert cell
expectEqual(multiply(12, 12), 144)
Insert cell
expectEqual(multiply(3, -2), -6)
Insert cell
Insert cell
Insert cell
// is 1 greater than 0? true!
1 > 0
Insert cell
// is 0 less than 1? true!
0 < 1
Insert cell
// 1 is equal to itself
// we use === to check equality
1 === 1
Insert cell
// 1 is not equal to 0, so this is false
1 === 0
Insert cell
Insert cell
Insert cell
Insert cell
message = userAge < 18
? 'Sorry, you’re not allowed to drink here ⛔'
: 'Here’s your beer! 🍺'
Insert cell
Insert cell
function getRandomMessage() {
// this gives a number between 0 and 1 with 2 decimal places
let randomNumber = Number(Math.random().toFixed(2))

// here's the ternary conditional statement...
let message = randomNumber > 0.5
? '💥 Sorry, too random!'
: '💩 Sorry, not random enough!'

return { randomNumber, message }
}
Insert cell
Insert cell
Insert cell
function booleanToString(bool) {
// use a ternary conditional statement, for example:
// return condition
// ? 'value if true'
// : 'value if false'
// tip 1: remember you can use triple-equals === for checking equality!
// tip 2: `bool` is already a boolean, so maybe you can use that directly?
return bool === true ? 'true' : 'false';

return 'to do!'
}
Insert cell
expectEqual(booleanToString(true), 'on')
Insert cell
expectEqual(booleanToString(false), 'off')
Insert cell
Insert cell
numArr = [1, 2, 3]
Insert cell
doubledArr = numArr.map(double)
Insert cell
expectEqual(doubledArr, [2, 4, 6])
Insert cell
strBools = [true, false, true].map(booleanToString)
Insert cell
expectEqual(strBools, ['on', 'off', 'on'])
Insert cell
arr.join(' // ')
Insert cell
numArr.map(double).join(' // ')
Insert cell
// '' is an empty string, so we join the array's items directly to each other, with no spaces
arr.join('')
Insert cell
Insert cell
arr2d = [
[1, 2, 3],
[4, 5, 6],
]
Insert cell
mappedArr2d = arr2d.map(function doubleAllCells(row) {
return row.map(double)
})
Insert cell
function doubleAllCells(row) {
return row.map(double)
}
Insert cell
mappedArr2dVersion2 = arr2d.map(doubleAllCells)
Insert cell
// this will automatically pass once the `double` function is fixed
expectEqual(mappedArr2d, [[2, 4, 6], [8, 10, 12]])
Insert cell
expectEqual(mappedArr2dVersion2, [[2, 4, 6], [8, 10, 12]])
Insert cell
Insert cell
Insert cell
Insert cell
// click the arrow above to see the individual rows and cells of the output
exampleQr = qr(example)
Insert cell
checkType(exampleQr)
Insert cell
Insert cell
exampleQr[0]
Insert cell
checkType(exampleQr[0])
Insert cell
Insert cell
exampleQr[0][0]
Insert cell
checkType(exampleQr[0][0])
Insert cell
Insert cell
function toBlock(bool) {
// block drawing characters

return bool ? '▓▓▓' : ' '
}
Insert cell
function renderBlocks(qrCode) {
// note: '\n' is the new-line character
// for example, printing 'hello\nworld' will print 'hello' on 1 line, then 'world' on the next line
// so joining with '\n' means we print each row on its own line
return qrCode.map(function(row) {
return row.map(toBlock).join('')
}).join('\n')
}
Insert cell
html`<pre>${renderBlocks(exampleQr)}</pre>`
Insert cell
Insert cell
emojiTrue = '🈵'
Insert cell
emojiFalse = '⬜'
Insert cell
function toEmoji(bool) {
// use the booleanToString and toBlock functions above as a reference

return 'to do!'
}
Insert cell
expectEqual(toEmoji(true), emojiTrue)
Insert cell
expectEqual(toEmoji(false), emojiFalse)
Insert cell
Insert cell
function renderEmojis(qrCode) {
// use renderBlocks function above as a reference

return 'to do!'
}
Insert cell
expectEqual(renderEmojis(exampleQr), exampleAnswer)
Insert cell
Insert cell
Insert cell
Insert cell
<p class="pink-background">This is a paragraph.</p>
Insert cell
Insert cell
Insert cell
html`<p>
<strong>Hello</strong>, <em>world</em>!
</p>`
Insert cell
Insert cell
<p>
<strong>Hello</strong>, <em>world</em>!
</p>
Insert cell
Insert cell
Insert cell
htmlParagraph = html``
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
html`<p>
7 + 9 = <strong>${7 + 9}</strong>
</p>`
Insert cell
Insert cell
function row1() {
return html`<div>
<span class="on"></span>
<span class="off"></span>
<span class="on"></span>
</div>`
}
Insert cell
function row2() {
return html`<div>
<span class="off"></span>
<span class="on"></span>
<span class="off"></span>
</div>`
}
Insert cell
html`<div class="qr-code">
${row1()}
${row2()}
${row1()}
</div>`
Insert cell
Insert cell
function toHtmlCell(bool) {
// how can we use conditional ternary statements here?
// hint - consider using any of these formats:

// return bool ? html`to do!` : html`to do!`
// let className = bool ? 'to do!' : 'to do!'
// return html`<span class="${className}"></span>`
// return html`<span class="${bool ? 'to do!' : 'to do!'}"></span>`

// which format seems best? which is most concise? which is easiest to read and understand?
return html`to do!`
}
Insert cell
expectEqual(
toHtmlCell(true),
html`<span class="on"></span>`,
)
Insert cell
expectEqual(
toHtmlCell(false),
html`<span class="off"></span>`,
)
Insert cell
function toHtmlRow(row) {
// this function should make use of toHtmlCell above.
// it should returning an HTML <div></div> element containing the cells for this row.
// remember ${...} for JS code within the HTML
// Also remember to use the array map method!
// row.map(...)
return html`to do!`
}
Insert cell
expectEqual(
toHtmlRow([true, false, true]),
html`
<div>
<span class="on"></span>
<span class="off"></span>
<span class="on"></span>
</div>
`,
)
Insert cell
expectEqual(
toHtmlRow([false, true, true]),
html`
<div>
<span class="off"></span>
<span class="on"></span>
<span class="on"></span>
</div>
`,
)
Insert cell
function renderQrCodeHtml(qrCode) {
// this function should call toHtmlRow above
// you can also use renderBlocks and renderEmojis functions above as a reference
// remember to use map here, too! but this time, you're mapping the rows of the qr code, not the cells of each row
// we'll use a div with a class attribute of "qr-code" as the outer HTML element for the whole QR code.

return html`to do!`
}
Insert cell
expectEqual(
renderQrCodeHtml([[true, false], [false, true]]),
html`
<div class="qr-code">
<div><span class="on"></span><span class="off"></span></div>
<div><span class="off"></span><span class="on"></span></div>
</div>
`,
)
Insert cell
expectEqual(
renderQrCodeHtml([[false, true], [true, true]]),
html`
<div class="qr-code">
<div><span class="off"></span><span class="on"></span></div>
<div><span class="on"></span><span class="on"></span></div>
</div>
`,
)
Insert cell
renderQrCodeHtml(qr(website))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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