Published
Edited
Feb 24, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Math.random()
Insert cell
Insert cell
function myRandom(min, max){
let range=max-min;
let rand = (Math.random() * range) + min;
return rand
}
Insert cell
// test
myRandom(2,8)
Insert cell
Insert cell
bestSeller=["harry porter", "atomic habit", "so good they can't ignore you"]
Insert cell
// title case and print the results we want
function bestSellerN(titleList, number){
let str=titleList[number-1].split(" ")
for (var i = 0; i<str.length; i++){
str[i]=str[i].charAt(0).toUpperCase() + str[i].slice(1)
}
titleList[number-1]=str.join(" ")
return "The number "+number+" bestseller today is: "+titleList[number-1]
}
Insert cell
Insert cell
//making the second input optional
function bestOptionalN(titleList, number) {
if(number == undefined) {
//if no input of number, output the best one
return bestSellerN(titleList, 1)
} else {
return bestSellerN(titleList, number)
}
}
Insert cell
Insert cell
//making the first input optional
function bestOptionalT(titleList, number) {
//if input type is number, we will judge that the first input is missiong.
if (typeof titleList=="number"){
return "The number "+titleList+" bestseller today is: (please input your list)"
}
else if (typeof titleList=="object"){
return bestOptionalN(titleList, number)
}
else {
return bestSellerN(titleList, number)
}
}
Insert cell
//test
bestOptionalT(2)
Insert cell
//making the first input optional
function bestOptional(titleList="empty", number) {
//if input type is number, we will judge that the first input is missiong.
if (typeof titleList=="string"){
return "Please input your list and Number"
}
else {
bestOptionalT(titleList, number)
}
}
Insert cell
Insert cell
Insert cell
function Password(password){
if (password.length< 8 || password.length> 14){
return "Error: password length must be 8-14 characters"
}
//"/\D/g" means doing a global search for non-digit characters
else if (password.replace(/\D/g, '').length < 2){
return "Error: password must contain at least 2 digits"
}
else if (password== password.toLowerCase){
return "Error: password must contain at least 1 uppercase letter"
}
else if (password.length== password.replace(/["!","@","#","$","%","^","&","*","(",")","-","_","+","="]/g, '').length ){
return "Error: password must contain at least 1 special character from this set !@#$%^&*()-_+="
}
else {
return "Password set successfully!"
}
}
Insert cell
Insert cell
//test
Password(passwordTest)
Insert cell
Insert cell
function exp(number, time){
let result=1
for(var i=1; i<=time; i++){
result=result*number
}
return result
}
Insert cell
//test
exp(2, 3)
Insert cell
Insert cell
// Your code here
listTest=[1,2,28,6,57]
Insert cell
//minimum function
function myMin(numberList){
let resultMin=numberList[0]
for(var i=0; i<numberList.length; i++){
if(numberList[i+1]<resultMin){
resultMin=numberList[i+1]
}
else{
resultMin=resultMin
}
}
return resultMin
}
Insert cell
//test
myMin(listTest)
Insert cell
//maximum function
function myMax(numberList){
let resultMax=numberList[0]
for(var i=0; i<numberList.length; i++){
if(numberList[i+1]>resultMax){
resultMax=numberList[i+1]
}
else{
resultMax=resultMax
}
}
return resultMax
}
Insert cell
//test
myMax(listTest)
Insert cell
Insert cell
{
var times=8
var word="pretty"
var outputWord="p r e t t y"
var blankSpaces="_ _ _ _ _ _"
var remains= word.length
var answerArray=[]
var roundWin=0
const form = html`
<form onsubmit="return false;">
<div>Input letter here: </div>
<div>
<input type=text name=inputText placeholder="input 1 lower-cased character at a time"></input>
</div>
<div>
<button name=OK>OK</button>
<button name=Reset>Reset</button>
</div>
<div>This is the current word: </div>
<input type=text name=current value="_ _ _ _ _ _"></input>
<div> Guessing times remained: </div>
<input type=text name=time value=8></input>
<div>Status: </div>
<div>
<input type=text name=hint></input>
</div>
</form>`
//define what reset function
form.Reset.onclick = () => {
times=8
answerArray=[]
roundWin=0
remains= word.length
form.inputText.value=""
form.current.value=blankSpaces
form.hint.value=""
form.time.value=8
}
//define one try
form.OK.onclick = () => {
if(times<=0||remains==0){return 0}
else{
var input=form.inputText.value
//testing the input
//if not valid: return error
if (input.length != 1) {
form.hint.value=("Please enter a single letter!")
return 0
}
else if (input.charCodeAt()<97 || input.charCodeAt()>122){
form.hint.value=("Please enter a lower-cased letter!")
return 0
}
// if valid: Update the game status with the guess
else {
roundWin=0
for (var j = 0; j < word.length; j++) {
if (word[j] == input && answerArray[j] != input) {
answerArray[j] = input
remains--
roundWin=1
}
}
// provide a more readable current word with spaces between the left characters
var outputAnswer=""
for (var i = 0; i < outputWord.length; i++) {
var x = outputWord.charAt(i);
if (x != " ") {
if(answerArray[i/2]==undefined){outputAnswer= outputAnswer+ "_"}
else{outputAnswer=outputAnswer+ answerArray[i/2]}
}
else {
outputAnswer= outputAnswer+ " "
}
}
form.current.value=outputAnswer
//count down trying times
times--
form.time.value=times
//judging win or lose
if (remains>0){
if(times>0){
if(roundWin==0){
form.hint.value=("Please try another letter.")
return 0
}
else{
form.hint.value=("")
return 0
}
}
else{
form.time.value=0
form.hint.value=("Game over! You lose!")
form.current.value=outputWord
return 0
}}
else{form.hint.value=("Congratulations! You win!")}
}}
}
return form
}
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