Published
Edited
Feb 25, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Math.random()
Insert cell
Insert cell
// Your code here
function random(max, min=0){
let range=max-min
return Math.random()*range+min //multiply random value between 1 and 0 with the range. Then add the min val.
}
Insert cell
// Your code here
function test(testVal, max, min=0){
return testVal>=min && testVal<=max //returns the truth value of the && statement
}
Insert cell
Insert cell
// Your code here
function bestseller1(titles, number){
let title = titles[number-1] //number is >= 1, the 0th bestseller does not exist
title = title.replace(/\w\S*/g, function(word){
return word.charAt(0).toUpperCase()+word.substr(1)
})
return 'The number '+number+' bestseller today is: '+title
}
Insert cell
// Second input optional, number is optional
function bestseller2(titles, number=false){
// if second input is given, execute bestseller1() code
if (number!= false){
let title = titles[number-1] //number is >= 1, the 0th bestseller does not exist
title = title.replace(/\w\S*/g, function(word){
return word.charAt(0).toUpperCase()+word.substr(1)
})
return 'The number '+number+' bestseller today is: '+title
}
// else, title-case titles and list them as bestsellers
else {
var newTitles = []
for (var t in titles){
newTitles.push(titles[t].replace(/\w\S*/g, function(word){
return word.charAt(0).toUpperCase()+word.substr(1)
}))
}
return 'The bestsellers today are: '+ newTitles.join(', ') + '.'
}
}
Insert cell
// First input optional, titles list optional
function bestseller3(number, titles=["new book list", "Hello Dear", "i hope you"]){
if (number <= titles.length){
let title = titles[number-1] //will take entries from titles(provided by user or not)
title = title.replace(/\w\S*/g, function(word){
return word.charAt(0).toUpperCase()+word.substr(1)
})
return 'The number '+number+' bestseller today is: '+title
}
// if the number is larger than the length of the book list, return "there is no number bestseller today."
else {
return 'There is no number '+number+' bestseller today.'
}
}
Insert cell
function bestseller4(titles=["new book list", "Hello Dear", "i hope you"], number=false){
if (number==false){
var newTitles = []
for (var t in titles){
newTitles.push(titles[t].replace(/\w\S*/g, function(word){
return word.charAt(0).toUpperCase()+word.substr(1)
}))
}
return 'The bestsellers today are: '+ newTitles.join(', ') + '.'
}
else {
if (number <= titles.length){
let title = titles[number-1] //will take entries from titles(provided by user or not)
title = title.replace(/\w\S*/g, function(word){
return word.charAt(0).toUpperCase()+word.substr(1)
})
return 'The number '+number+' bestseller today is: '+title
}
// if the number is larger than the length of the book list, return "there is no number bestseller today."
else {
return 'There is no number '+number+' bestseller today.'
}
}
}
Insert cell
Insert cell
// Your code here
function validation(password){
//given password, will throw error message detailing what is wrong with the password. Otherwise, returns message
const specialChars = "!@#$%^&*()-_+="
const len = password.length
const chars = password.split('')
const correctLen = (8<=len<=14)
let charNums = 0
let containsUC = false
let containsSC = false
let errorMessage = ""
//loop through password to check for conditions
for (var c in chars){
if (isNaN(chars[c]) == false){charNums += 1}
if (containsUC == false){containsUC = (chars[c] == chars[c].toUpperCase())}
if (containsSC == false){containsSC = specialChars.includes(chars[c])}
}
//constructing error message
if (!correctLen){errorMessage += "wrong length; "}
if (charNums<2){errorMessage += "too few digits; "}
if (!containsUC){errorMessage += "missing a uppercase letter; "}
if (!containsSC){errorMessage += "missing a special character; "}
//raising an error or not
if (errorMessage != ""){throw "Error: " + errorMessage}
return "great password!"
}
Insert cell
Insert cell
// Your code here
function exp(d1, d2){
if (d2 == 1){ //base case
return d1
}
else {
return d1 * exp(d1, d2-1)
}
}
Insert cell
Insert cell
// Your code here: minimum function
function min(array){
//set initial smallest value to first object in array
let smallest = array[0]
//iterate over array to replace smallest whenever a smaller value appears
for (var a in array){
if (array[a]<smallest){smallest = array[a]}
}
return smallest
}
Insert cell
// Your code here: minimum function
function max(array){
//set initial smallest value to first object in array
let largest = array[0]
//iterate over array to replace largest whenever a larger value appears
for (var a in array){
if (array[a]>largest){largest = array[a]}
}
return largest
}
Insert cell
Insert cell
// Your code here

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