Published
Edited
Mar 2, 2021
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
// Import the addition or multiplication function from this notebook
//Notebook Link: https://observablehq.com/@rakshith/practice-examples-for-functions-and-import
Insert cell
//Code to import
import { addition } from "@rakshith/practice-examples-for-functions-and-import"
Insert cell
//Call the function with 2 input values to display the result
//ex: division(2,6)
addition(100,20)
Insert cell
Insert cell
//Declare a global variable
my_variable = 10
Insert cell
//Write a function to get the divison of 2 numbers
//example 1

function divison(a,b)
{
return a/b
}
Insert cell
// Call the function

divison(4,2)
Insert cell
Insert cell
Insert cell
//Create a global array
my_array_variable = [1,'a',3.5]
Insert cell
//Create a nested array - Array inside an array
//example 1
my_nested_variable = [ [1,'z',3] , ['a','b','c']]
Insert cell
function nested_variable()
{//Create a function and Access the above nested array in this function and return it using the return keyword
//example 2
return my_nested_variable
}
Insert cell
//call the above function in here
nested_variable()
Insert cell
Insert cell
function sample()
{
var v = [1,4,7]
return v
}
Insert cell
function example(a= sample())
{
return a
}
Insert cell
example()
Insert cell
Insert cell
{
var d = { p1:null , p2:'d' , p3:145}
return d
}
Insert cell
//Create a Global objects with 2 properties(values) to be displayed
//example 1
Insert cell
v = ( { p1: 'abc', p2: 123 })
Insert cell
v['p1']
Insert cell
// Create an object with 2 properties and 1 method
//example 2
function my_object_1(a)
{
var my_object = { property1: 'hi', property2: 'bye'}
if (a == 'my_object')
{ return my_object }
else
{ return 'incorrect input'}
}
Insert cell
my_object_1('my_object')
Insert cell
Insert cell
function object_method()
{
return my_object_1('my_object')
}
Insert cell
object_method()
Insert cell
md `### Arrays, Objects with functions`
Insert cell
//Create an array with Objects inside it
array_with_objects = [ { dept: 'dh', course_number: '530'}, { dept: 'ece', course_number: 720}]
Insert cell
//Object inside an Object
nested_objects = ( { dept: 'library', nested_object: { p1: 'books'} ,course_number: 255, dept: 'admin', course_number: 540 } )
Insert cell
//Create an Object with arrays inside
object_with_arrays_inside = ( { arr1: [10,20,[ 2,5,8 ]], arr2:['z','y','x' ]} )

Insert cell
md `### Import any bar chart from vega--lite and display the data from "samlpe3@1.csv" file
* The file can be found in the attached files section`
Insert cell
//Code
import { vl } from "@vega/vega-lite-api"
Insert cell
input_data = FileAttachment("alphabet.csv").csv() // about file attachments https://observablehq.com/@observablehq/file-attachments
Insert cell
// about viewof https://observablehq.com/@observablehq/a-brief-introduction-to-viewof
viewof my_bar_chart = vl.markBar() // Make a bar chart
.data(input_data) // Using the alphabet data
.encode(
vl.x().fieldO("letter"), // .sort(vl.fieldQ("frequency").order("descending")), // Letters are ordinal on the x-axis
vl.y().fieldQ("frequency").axis({ format: "%" }) // Frequency on the y-axis, formatted as percent
)
.render()
Insert cell
Insert cell
//Sample
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