Published
Edited
Feb 23, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Math.random()
Insert cell
Insert cell
// 1. a
function randomInteger(high, low=0){
return Math.floor(Math.random() *(high - low +1)) + low;
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
Insert cell
randomInteger(4,50) // the order here does not matter
Insert cell
// 1. b

function testerFunction (test, min, max){ // function with 3 inputs
if (test > min && test < max){ // && = AND
return 'True'
}
else{
return 'False'
}
}
Insert cell
testerFunction(3, 0, 10) // True because the test value is between the min and the max values
Insert cell
testerFunction(3, 10, 100) // False the test its lower that the min
Insert cell
Insert cell
function bestBook(array, N) {
array = array.toLowerCase() // convert all the array in lower case
array = array.split(" ") // split string
for (var i = 0; i < array.length; i++) { // loop function to scan the array
array[i] = array[i].charAt(0).toUpperCase() + array[i].slice(1); //title case the array
}
array = array.join(" ") // join string
return `The number ${N} bestseller today is: ${array}` // print
}
//Source: https://stackoverflow.com/questions/18348422/javascript-forvar-i-array-length-v-s-for-e-in-array
Insert cell
bestBook('I am coding', 1) // I do not understand why it generates an error if the first input is an array?
Insert cell
function numberInputOpt(string, number=2) {
return bestBook(string, number) // return initial function with selected string + default number
}
Insert cell
numberInputOpt('Coding') // specifying just the string the number will be the default value
Insert cell
function stringInputOpt(number, string = 'DUSP MIT') {
return bestBook(string, number) // return initial function with selected number + default string
}
Insert cell
stringInputOpt(30) // specifying just the number the string will be the default value
Insert cell
function bothInputOpt(number = 20, string = 'DUSP MIT') {
return bestBook(string, number) // return the bestBook function with both inputs as defaulted above
}
Insert cell
bothInputOpt() // no inputs here = default values
Insert cell
Insert cell
function passwordValidation(string){
var digits=0 // numeric counter beginning in 0
var upperCase=0 //upper case counter beginning in 0
var specialCharacter=0 //special character counter beginning in 0
for (var i = 0; i < string.length; i++) { //scan the following
if (parseInt(string[i]) >=0){ // test and count integers
digits= digits+1
}
if (string[i] == '!'||string[i] == '@'||string[i] == '#'||string[i] == '$'||string[i] == '%'
||string[i] == '^'||string[i] == '&'||string[i] == '*'||string[i] == '('||string[i] == ')'||string[i] == '-'||string[i] == '_'||string[i] == '+'||string[i] == '='){ // test and count special characters
specialCharacter=specialCharacter+1
}
if (string[i] == string[i].toUpperCase()) { // test and count upper cases
upperCase=upperCase+1
}
}
upperCase = upperCase-digits// we need to subtract digits from the count of upper cases because they pass the upper case test
if (string.length > 7 && string.length < 15 && digits >1 && upperCase>0 &&specialCharacter>0){
return 'Congratulations your password is awesome!'
}
else {
return 'Try Again Please!'
}
}
// source: https://stackoverflow.com/questions/16140379/javascript-password-validation
Insert cell
passwordValidation("HeyLetMeIn?")
Insert cell
passwordValidation("Natalia2019*")
Insert cell
Insert cell
function expFunction (base, exponent){
return Math.pow(base, exponent)
}
// source: https://www.geeksforgeeks.org/javascript-math-pow-function/
Insert cell
expFunction(5, 4)
Insert cell
expFunction(10, 5)
Insert cell
expFunction(7,0)
Insert cell
Insert cell
list = [3, 7, 15, 1, 57]
Insert cell

function minMax(list){
var min = list[0]
for (var i = 0; i < list.length; i++){ // looping to find the min element
if (list[i] < min) // if the element being checked is lower than the previous,
min = list[i]; // min equals that element
};
var max = list[0]
for (var i = 0; i < list.length; i++){ // looping to find the max element
if (list[i] > max) // if the element being checked is higher than the previous,
max = list[i]; // max equals that element
};
return min, max; // It works when I put just min, just max, and min + max! Why does not return both?
};
Insert cell
minMax(list)
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