Basic JavaScript 090
61-Comparison with the Less Than Or Equal To Operator
function testLessOrEqual(val) {
if (val <= 12) { // Change this line
return "Smaller Than or Equal to 12";
}
if (val <= 24) { // Change this line
return "Smaller Than or Equal to 24";
}
return "25 or More";
}
// Change this value to test
testLessOrEqual(10);
62-Comparisons with the Logical And Operator
function testLogicalAnd(val) {
// Only change code below this line
if ( val<=50 && val>= 25 ) {
return "Yes";
}
// Only change code above this line
return "No";
}
// Change this value to test
testLogicalAnd(10);
63-Comparisons with the Logical Or Operator
function testLogicalOr(val) {
// Only change code below this line
if (val<10 || val>20) {
return "Outside";
}
// Only change code above this line
return "Inside";
}
// Change this value to test
testLogicalOr(15);
64-Introducing Else Statements
function testElse(val) {
var result = "";
// Only change code below this line
if (val > 5) {
result = "Bigger than 5";
} else {
result = "5 or Smaller";
}
// Only change code above this line
return result;
}
// Change this value to test
testElse(4);
65-Introducing Else If Statements
function testElseIf(val) {
if (val > 10) {
return "Greater than 10";
}
else if(val < 5) {
return "Smaller than 5";
}
else{
return "Between 5 and 10";
}
}
// Change this value to test
testElseIf(7);
66-Logical Order in If Else Statements
function orderMyLogic(val) {
if (val < 5) {
return "Less than 5";
} else if (val < 10) {
return "Less than 10";
} else {
return "Greater than or equal to 10";
}
}
// Change this value to test
orderMyLogic(7);
67-Chaining If Else Statements
function testSize(num) {
// Only change code below this line
var result = "";
if (num>=20){
result = "Huge";
}
else if (num<5){
result = "Tiny";
}
else if (num<10){
result = "Small";
}
else if (num<15){
result = "Medium";
}
else if (num<20){
result = "Large";
}
return result;
// Only change code above this line
}
// Change this value to test
testSize(19);
68-Golf Code
function golfScore(par, strokes) {
// Only change code below this line
var result = "";
if (strokes==1){
result = "Hole-in-one!";
}
else if (strokes<=par-2){
result = "Eagle";
}
else if (strokes==par-1){
result = "Birdie";
}
else if (strokes===par){
result = "Par";
}
else if (strokes==par+1){
result = "Bogey";
}
else if (strokes==par+2){
result = "Double Bogey";
}
else if (strokes>=par+3){
result = "Go Home!";
}
return result;
// Only change code above this line
}
// Change these values to test
golfScore(5, 4);
69-Selecting from many options with Switch Statements
function caseInSwitch(val) {
var answer = "";
// Only change code below this line
switch (val){
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
answer = "delta";
break;
}
// Only change code above this line
return answer;
}
// Change this value to test
caseInSwitch(1);
70-Adding a default option in Switch statements
function switchOfStuff(val) {
var answer = "";
// Only change code below this line
switch(val){
case "a":
answer = "apple";
break;
case "b":
answer = "bird";
break;
case "c":
answer = "cat";
break;
default:
answer = "stuff";
}
// Only change code above this line
return answer;
}
// Change this value to test
switchOfStuff("a");
71-Multiple Identical Options in Switch Statements
function sequentialSizes(val) {
var answer = "";
// Only change code below this line
switch(val){
case 1:
case 2:
case 3:
answer = "Low";
break;
case 4:
case 5:
case 6:
answer = "Mid";
break;
case 7:
case 8:
case 9:
answer = "High";
break;
}
// Only change code above this line
return answer;
}
// Change this value to test
sequentialSizes(1);
72-Replacing If Else Chains with Switch
function chainToSwitch(val) {
var answer = "";
// Only change code below this line
switch(val){
case "bob":
answer = "Marley";
break;
case 42:
answer = "The Answer";
break;
case 1:
answer = "There is no #1";
break;
case 99:
answer = "Missed me by this much!";
break;
case 7:
answer = "Ate Nine";
break;
}
// Only change code above this line
return answer;
}
// Change this value to test
chainToSwitch(7);
73-Returning Boolean Values from Functions
function isLess(a, b) {
// Fix this code
return a < b;
}
// Change these values to test
isLess(10, 15);
74-Return Early Pattern for Functions
// Setup
function abTest(a, b) {
// Only change code below this line
if (a<0 || b<0){
return undefined;
}
// Only change code above this line
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}
// Change values below to test your code
abTest(2,2);
75-Counting Cards
var count = 0;
function cc(card) {
// Only change code below this line
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
count = count + 1;
break;
case 7:
case 8:
case 9:
break;
case 10:
case "J":
case "Q":
case "K":
case "A":
count = count -1;
break;
}
if(count===0){
return "0 Hold";
}
else if(count>0){
return count + " Bet";
}
else if(count<0){
return count + " Hold";
}
// Only change code above this line
}
// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');
76-Build JavaScript Objects
// Example
var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};
// Only change code below this line.
var myDog = {
"name": "snoopy",
"legs": 2,
"tails": 1,
"friends": ["lala", "xixi"]
};
77-Accessing Objects Properties with the Dot Operator
// Setup
var testObj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
// Only change code below this line
var hatValue = testObj.hat; // Change this line
var shirtValue = testObj.shirt; // Change this line
78-Accessing Objects Properties with Bracket Notation
// Setup
var testObj = {
"an entree": "hamburger",
"my side": "veggies",
"the drink": "water"
};
// Only change code below this line
var entreeValue = testObj["an entree"]; // Change this line
var drinkValue = testObj["the drink"]; // Change this line
79-Accessing Objects Properties with Variables
// Setup
var testObj = {
12: "Namath",
16: "Montana",
19: "Unitas"
};
// Only change code below this line;
var playerNumber = 16; // Change this Line
var player = testObj[playerNumber]; // Change this Line
80-Updating Object Properties
// Example
var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};
ourDog.name = "Happy Camper";
// Setup
var myDog = {
"name": "Coder",
"legs": 4,
"tails": 1,
"friends": ["Free Code Camp Campers"]
};
myDog.name = "Happy Coder";
// Only change code below this line.
81-Add New Properties to a JavaScript Object
// Example
var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};
ourDog.bark = "bow-wow";
// Setup
var myDog = {
"name": "Happy Coder",
"legs": 4,
"tails": 1,
"friends": ["Free Code Camp Campers"]
};
myDog.bark = "woof";
// Only change code below this line.
82-Delete Properties from a JavaScript Object
// Example
var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"],
"bark": "bow-wow"
};
delete ourDog.bark;
// Setup
var myDog = {
"name": "Happy Coder",
"legs": 4,
"tails": 1,
"friends": ["Free Code Camp Campers"],
"bark": "woof"
};
delete myDog.tails;
// Only change code below this line.
83-Using Objects for Lookups
// Setup
function phoneticLookup(val) {
var result = "";
// Only change code below this line
var lookup = {
alpha:"Adams",
bravo: "Boston",
charlie: "Chicago",
delta: "Denver",
echo: "Easy",
foxtrot: "Frank"
};
result = lookup[val];
// Only change code above this line
return result;
}
// Change this value to test
phoneticLookup("charlie");
84-Testing Objects for Properties
不能用 dot 要用 bra陣列陣列一樣嘛ket notation,有人說是因為資料存在變數裡面(就像是)
// 初始化变量
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
// 请把你的代码写在这条注释以下
IfInthere = myObj.hasOwnProperty(checkProp);
result = "";
if(IfInthere){
result = myObj[checkProp];
return result;
}
else {
result = "Not Found";
return result;
}
}
// 你可以修改这一行来测试你的代码
checkObj("gift");
---
85-Manipulating Complex Objects
```js
var myMusic = [
{
"artist": "Billy Joel",
"title": "Piano Man",
"release_year": 1973,
"formats": [
"CS",
"8T",
"LP" ],
"gold": true
},
// Add record here
{
"artist": "Chiu",
"title": "SMS life",
"release_year": 2017,
"formats": ["sad", "angry", "helpless"]
}
];
86-Accessing Nested Objects
- 有空格的屬性,用
// Setup
var myStorage = {
"car": {
"inside": {
"glove box": "maps",
"passenger seat": "crumbs"
},
"outside": {
"trunk": "jack"
}
}
};
// Only change code below this line
var gloveBoxContents = myStorage.car.inside["glove box"]; // Change this line
87-Accessing Nested Arrays
物件的屬性存取可以用 dot 或是 bracket notation
陣列的內容存取只能用 []
// Setup
var myPlants = [
{
type: "flowers",
list: [
"rose",
"tulip",
"dandelion"
]
},
{
type: "trees",
list: [
"fir",
"pine",
"birch"
]
}
];
// Only change code below this line
var secondTree = myPlants[1].list[1]; // Change this line
88-Iterate with JavaScript For Loops
for ([initialization]; [condition]; [final-expression])
// Example
var ourArray = [];
for (var i = 0; i < 5; i++) {
ourArray.push(i);
}
// Setup
var myArray = [];
for (var i = 1; i < 6; i++){
myArray.push(i);
}
// Only change code below this line.
89-Iterate Odd Numbers With a For Loop
// Example
var ourArray = [];
for (var i = 0; i < 10; i += 2) {
ourArray.push(i);
}
// Setup
var myArray = [];
for (var i = 1; i < 10; i += 2){
myArray.push(i);
}
// Only change code below this line.
90-Count Backwards With a For Loop
// Example
var ourArray = [];
for (var i = 10; i > 0; i -= 2) {
ourArray.push(i);
}
// Setup
var myArray = [];
// Only change code below this line.
for (var i=9; i>0; i-=2){
myArray.push(i);
}
Last updated
Was this helpful?