Published
Edited
Feb 24, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Math.random()
Insert cell
Insert cell
// Your code here
function rand_num(max, min = 0){
return Math.floor(Math.random() * (max - min)) + min; // generates a float between 0 and 1, then multiples the maximum minus the minimum. If Math.random is 1, then it is like saying 1*(max-min+min) and if it is 0, it is like saying 0+min
}
Insert cell
// Your code here
function rand_num_test(max, min = 0){
var test_num = rand_num(max, min); // run the function
if (test_num > min && test_num < max){ // test if it is in the range
return true;
} else {
return false;
}
}

Insert cell
rand_num_test(500,100)
Insert cell
Insert cell
// Your code here
function stringin_along(a,b){
var llength = a.length;
console.log(llength);
for (let i=0; i<=llength; ++i){ // loop through
let title_low = a[i].toLowerCase() // set to lowercase
let title = title_low.charAt(0).toUpperCase() + title_low.slice(1) // concat the string with first char upper, rest lower
return (`The number ${i+1} bestseller today is: ${title}`) // return the string
}
}
Insert cell
stringin_along(['abc','b','c'],1)
Insert cell
// Your code here
function stringin_along_b(a,b=0){
var llength = a.length;
console.log(llength);
for (let i=0; i<=llength; ++i){
let title_low = a[i].toLowerCase()
let title = title_low.charAt(0).toUpperCase() + title_low.slice(1)
return (`The number ${i+1} bestseller today is: ${title}`)
}
}
Insert cell
stringin_along_b(['abc','b','c'])
Insert cell
// Your code here
function stringin_along_c(b,a=['Born in the USA','Solja Boi',"Rachmaninoff's 4th"]){
var llength = a.length;
console.log(llength);
for (let i=0; i<=llength; ++i){
let title_low = a[i].toLowerCase()
let title = title_low.charAt(0).toUpperCase() + title_low.slice(1)
return (`The number ${i+1} bestseller today is: ${title}`)
}
}
Insert cell
stringin_along_c(0)
Insert cell
// Your code here
function stringin_along_d(a=['Born in the USA','Solja Boi',"Rachmaninoff's 4th"],b=0){
var llength = a.length;
console.log(llength);
for (let i=0; i<=llength; ++i){
let title_low = a[i].toLowerCase()
let title = title_low.charAt(0).toUpperCase() + title_low.slice(1)
return (`The number ${i+1} bestseller today is: ${title}`)
}
}
Insert cell
stringin_along_d();
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Your code here
function exponent_calc(int,exp){
var total = int; // delcare coutner
while (Math.abs(exp)>1){ //abs for positive and negative exp
total *= int; // multiply to coutner
exp-=1;
}
if (exp>=1) { // if/elif for negative or 0 exponents. this doesnt account for fraction or negative fraction exponents
return total;
} else if (exp==0) { // 0 expnonent
return 1 }
else { // negative exponents
return 1/total;
}
}
Insert cell
exponent_calc(8,3)
Insert cell
exponent_calc(4,-1)
Insert cell
Insert cell
// Your code here
function get_min(nums){
var min = nums[0]; // slice first number as starting point
for(let i=1; i<=nums.length-1; ++i){ // declare for loop, limit length of array
if (nums[i]<min) {
min = nums[i]; // redefine min if less than current min
} else {}; // else do nada
}
return min;
}
Insert cell
function get_max(nums){
var max = nums[0]; // slice first number as starting point
for(let i=1; i<=nums.length-1; ++i){
if (nums[i]>max) {
max = nums[i]; // redefine max if more than current min
} else {}; // else do nada
}
return max;
}
Insert cell
get_min([5,4,3,2,1,0,-1,2,3])
Insert cell
get_max([5,4,3,2,1,0,-1,2,3])
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