Published
Edited
Feb 26, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Math.random()
Insert cell
Insert cell
// how to call without min?
// how to have all in one cell?
// what errors are we testing?
Insert cell
viewof min = html`<input type=number placeholder="Min value">`;
Insert cell
viewof max = html`<input type=number placeholder="Max value">`;
Insert cell
function userRandom(max, min=0){
return Math.floor(Math.random() * ((max - min) + 1) + min);
}

Insert cell
userRandom(max)
Insert cell
function testUserRandom (min,max){ //** what ?
if (min != min) {
min = 0;
}
return Math.floor(Math.random() * ((max - min) + 1) + min);
}

Insert cell
Insert cell
// are we supposed to call the function with only 1 argument?
Insert cell
viewof myTitles = html`<input type=text placeholder="Enter titles separated by slash">`;
Insert cell
viewof myNumber = html`<input type=number placeholder="Enter your favorite number">`;
Insert cell
function bestSeller (myTitles, myNumber) {
var N = Math.floor(Math.random() * (((myNumber*2) - myNumber) + 1) + myNumber); // random number based on input
var arrayTitle = myTitles.split("/");
var arrayIndex = Math.floor(Math.random() * (((arrayTitle.length-1) - 0) + 1) + 0); // random array Index
return "The number "+ (N) + " bestseller today is: " + titleCase(arrayTitle[arrayIndex]);
}
Insert cell
bestSeller(myTitles, myNumber)
Insert cell
function bestSeller_no2nd (myTitles, myNumber=3) {
var N = Math.floor(Math.random() * (((myNumber*2) - myNumber) + 1) + myNumber); // random number based on input
var arrayTitle = myTitles.split("/");
var arrayIndex = Math.floor(Math.random() * (((arrayTitle.length-1) - 0) + 1) + 0); // random array Index
return "The number "+ (N) + " bestseller today is: " + titleCase(arrayTitle[arrayIndex]);
}
Insert cell
bestSeller_no2nd (myTitles)
Insert cell
function bestSeller_no1st (myTitles, myNumber) {
if (myTitles == "") {
myTitles = "Hello world";
var N = Math.floor(Math.random() * (((myNumber*2) - myNumber) + 1) + myNumber);
var arrayTitle = myTitles.split();
var arrayIndex = 0;
}
else {
var N = Math.floor(Math.random() * (((myNumber*2) - myNumber) + 1) + myNumber); // random number based on input
var arrayTitle = myTitles.split("/");
var arrayIndex = Math.floor(Math.random() * (((arrayTitle.length-1) - 0) + 1) + 0); // random array Index
}
return "The number "+ (N) + " bestseller today is: " + titleCase(arrayTitle[arrayIndex]);
}
Insert cell
bestSeller_no1st (myTitles, myNumber)
Insert cell
function bestSeller_noInput (myTitles, myNumber=3) {
if (myTitles == "") {
myTitles = "Hello jupter";
var N = Math.floor(Math.random() * (((myNumber*2) - myNumber) + 1) + myNumber);
var arrayTitle = myTitles.split();
var arrayIndex = 0;
}

else {
var N = Math.floor(Math.random() * (((myNumber*2) - myNumber) + 1) + myNumber); // random number based on input
var arrayTitle = myTitles.split("/");
var arrayIndex = Math.floor(Math.random() * (((arrayTitle.length-1) - 0) + 1) + 0); // random array Index
}
return "The number "+ (N) + " bestseller today is: " + titleCase(arrayTitle[arrayIndex]);
}
Insert cell
bestSeller_noInput(myTitles)
Insert cell
Insert cell
// should the code wait for pushing "enter" button?
Insert cell
viewof password1 = html`<input type=password placeholder="Enter your password">`;
Insert cell
function passwordValid(password) {
var digits = [0,1,2,3,4,5,6,7,8,9];
var specialChar = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "="];
var containsSpecialChar = false;
var containsDigit = 0;
var containsUpper = false;
var passArray = password.split("");
for(var i=0; i< specialChar.length; i++){
if(password.includes(specialChar[i]))
containsSpecialChar = true;
}
for(var i=0; i< passArray.length; i++){
if(passArray[i] == passArray[i].toUpperCase())
containsUpper = true;
}
for(var i=0; i< digits.length; i++){
if(password.includes(digits[i]))
containsDigit++;
}
if (password.length < 8)
return html`<span style="color: red;"> Password must contain 8-14 characters. </span`;
if (password.length > 14)
return html`<span style="color: red;"> Password must contain 8-14 characters. </span`;
if(!containsUpper)
return html`<span style="color: red;"> Password must include at least 1 Uppercase letter. </span`;
if(containsDigit < 2)
return html`<span style="color: red;"> Password must include at least 2 digits. </span`;
if(!containsSpecialChar)
return html`<span style="color: red;"> Password must include special characters. </span`;
else {
return html`<span style="color: green;"> Success! You are all set! </span`;
}
}
Insert cell
passwordValid(password1)
Insert cell
specialChar = ["#","$"];
Insert cell
testChar = ["#", "a", "b"];
Insert cell
testChar.includes(specialChar[0,2])
Insert cell
Insert cell
function recursExp(a,b) {
if (b == 1) { // base case
return a;
}
else {
return a * recursExp(a,b-1);
}
}
Insert cell
Insert cell
array1 = [15,5,-5,-3,20]
Insert cell
function myMin(arrayToTest){
var N = Infinity;
for (var i = 0; i < arrayToTest.length; i++) { // looping in each element
if (arrayToTest[i] < N){
N = arrayToTest[i] ;
}
}
return N;
}
Insert cell
myMin(array1)
Insert cell
function myMax(arrayToTest){
var N = -Infinity;
for (var i = 0; i < arrayToTest.length; i++) { // looping in each element
if (arrayToTest[i] > N){
N = arrayToTest[i] ;
}
}
return N;
}
Insert cell
myMax(array1)
Insert cell
Insert cell
// Your code here

Insert cell
Insert cell
function titleCase(str) {
str = str.toLowerCase();
str = str.split(' ');
for (var i = 0; i < str.length; i++) {
str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1);
}
return str.join(' ');
}


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