Published
Edited
Feb 25, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Math.random()
Insert cell
Insert cell
/*
Implement your own random function that builds on this function,
returning an integer between a low and a high number supplied by the user,
but that can be called with the low number optional (default to 0).
*/
function myRandInt(low=0, high) { // Set default for low value to 0, but high value must be set
let r = Math.random(); // Random number between 0 and 1
let intr = Math.round((high-low)*r); // Multiply r so the random interval is the right width and round so it's integer
return low + intr; // Shift interval by adding low number
}
Insert cell
/*
Show us that your function works by creating a tester function that uses logical statements to test that returns true if the function worked correctly. The inputs for the function should be the value to test, and the min and max values that you are testing out.
*/
function isValueInRange(testVal, min, max) {
// Return true if min < testVal < max, false otherwise
if (testVal < min || testVal > max) {
return false;
} else {
return true;
}
}
Insert cell
// Test whether random integer generated is between specified low and high value
isValueInRange(myRandInt(2,18),2,18)
Insert cell
Insert cell
// First function with two mandatory inputs
function myBookReport(titles, index) { // titles is a list of strings of bestseller names, index is the number bestseller to look up
let formattedTitle = convertToTitleCase(titles[index-1]); // For the #1 bestseller, should access the 0th element of titles
return `The number ${index} bestseller today is: ${formattedTitle}`;
}
Insert cell
Insert cell
// Show that it works...
myBookReport(titles, 3)
Insert cell
// Second funtion, with second input optional
// Index defaults to 1, so if user does not specify input the 1st book is returned
function myBookReportDefaultIndex(titles, index=1) {
return myBookReport(titles, index);
}
Insert cell
// Show the second function works...
myBookReportDefaultIndex(titles)
Insert cell
// Third function, with first input (list of titles) optional
// Titles argument defaults to 0, and conditional argument will return "Book N" if titles = 0
function myBookReportDefaultTitles(index, titles=0) {
if (titles === 0) {
return `The number ${index} bestseller today is: Book No. ${index}`;
}
else {
return myBookReport(titles, index);
}
}
Insert cell
// Show the third function works...
myBookReportDefaultTitles(5)
Insert cell
// Fourth function with either input optional
// Titles argument defaults to 0, and conditional will return "Book No. N" if titles = 0
// Index argument defaults to 1, so 1st bestseller will be returned if index not specified
function myBookReportAllDefaults(titles=0, index=1) {
if (titles === 0) {
return `The number ${index} bestseller today is: Book No. ${index}`;
}
else {
return myBookReport(titles, index)
}
}
Insert cell
// Show the fourth function works...
myBookReportAllDefaults()
Insert cell
// Helper function to deal with converting to title case
// Based on https://medium.freecodecamp.org/three-ways-to-title-case-a-sentence-in-javascript-676a9175eb27
function convertToTitleCase(string) { // Convert a string to title case, i.e. capitalize every word
let words = string.split(' ');
let titleCaseWords = [];
for (var i in words) {
titleCaseWords[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
return titleCaseWords.join(' ')
}
Insert cell
Insert cell
// Your code here
function validatePassword(password) {
let passwordWorks = true; // Start with password working set to true, and set it to false if it fails a test
// Password length must be 8-14 characters
if (password.length < 8) {
passwordWorks = false;
}
if (password.length > 14) {
passwordWorks = false;
}
// Reference list of accepted special characters
const specialDict = "!@#$%^&*()-_+=";
// List of characters that fit each category in the password, to be assigned later
let digits = [];
let capitals = [];
let specials = [];
// Iterate through string and store numerals, capitals, and special characters
for (var i in password) {
let char = password.charAt(i);
// Check if char is uppercase (i.e. converting to uppercase doesn't change it)
// It also needs to be a letter, so it should be different when converted to lowercase
if (char === char.toUpperCase() && char != char.toLowerCase()) {
capitals.push(char);
}
// Check if char is a special character by referencing list of specials
// If char is not in specialDict, indexOf returns -1. Otherwise will return nonnegative index.
if (specialDict.indexOf(char) >= 0) {
specials.push(char);
}
// Check if char is a digit
// Using code from https://stackoverflow.com/a/14164576
// If char isn't a digit, the regex will find no match and char.match will return null
if (char.match(/\d/g) != null) {
digits.push(char);
}
}
// Password must contain at least 2 digits
if (digits.length < 2) {
passwordWorks = false;
}
// Password must contain at least 1 special character
if (specials.length < 1) {
passwordWorks = false;
}
// Password must contain at least 1 uppercase letter
if (capitals.length < 1) {
passwordWorks = false;
}
if (passwordWorks) {
return "Password meets all requirements!";
}
else {
return "ERROR: Password fails one or more requirements"; // Could be improved by specifying which test fails
}
}
Insert cell
// Use a test password to test the function - no symbol, this should fail
validatePassword("numBers1234")
Insert cell
// Another - too short, this should fail
validatePassword("sH0rt%8")
Insert cell
// Another - too long, this should fail
validatePassword("veryveryveryVeryverylong15&")
Insert cell
// Another - not enough numbers, this should fail
validatePassword("A%1thisfails")
Insert cell
// Another - no capital, this should fail
validatePassword("p@ssword71")
Insert cell
// Another - this should work
validatePassword("p@ssw0rD1")
Insert cell
Insert cell
// Base is an integer that will be recursively multiplied, and power is an integer that defines the number of times to multiply the base
function myIterativeExp(base, power) {
// Returns base**power by using iteration
// Start with 1
let output = 1;
// Sets power # of loops of...
for (let i = 0; i < power; i++) {
// ... multiplying 1 by output each time
output = output * base;
}
return output;
}
Insert cell
// Testing with one of the specified test cases
myIterativeExp(5, 4)
Insert cell
Insert cell
// Takes a list of numbers and returns the minimum element of that list
function myMin(numList) {
// Start min at infinity
let min = Infinity;
// Iterate through the input list of numbers
for (var i in numList) {
// If the ith element is less than the current min, overwrite the current min with this element
if (numList[i] < min) {
min = numList[i];
}
}
return min;
}
Insert cell
Insert cell
// Takes a list of numbers and returns the maximum element of that list
function myMax(numList) {
// Start max at negative infinity
let max = -1*Infinity;
// Iterate through the input list of numbers
for (var i in numList) {
// If the ith element is greater than the current max, overwrite the current max with this element
if (numList[i] > max) {
max = numList[i];
}
}
return max;
}
Insert cell
// Test max
myMax([-100,-80,-103,-55])
Insert cell
Insert cell
// Your code here

Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more