freecode Basic JavaScript(下)

54.Assignment with a Returned Value

// Setup
var processed = 0;

function processArg(num) {
  return (num + 3) / 5;
}

// Only change code below this line
var processed=processArg(7) 

55.Stand in Line(review) //array.shift() return the first element of the original array

function nextInLine(arr, item) {
  // Only change code below this line
  arr.push(item)
  var remover = arr.shift();
  return remover;
  // Only change code above this line
  

}

// Setup
var testArr = [1,2,3,4,5];

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

56.Understanding Boolean Values

function welcomeToBooleans() {

  // Only change code below this line

  return true; // Change this line

  // Only change code above this line
}

57.Use Conditional Logic with If Statements

function trueOrFalse(wasThatTrue) {
  // Only change code below this line
  if(wasThatTrue){
    return "Yes, that was true"
  }
  return "No, that was false"
  // Only change code above this line

}

58.Comparison with the Equality Operator

// Setup
function testEqual(val) {
  if (val==12) { // Change this line
    return "Equal";
  }
  return "Not Equal";
}

testEqual(10);

59.Comparison with the Strict Equality Operator

// Setup
function testStrict(val) {
  if (val===7) { // Change this line
    return "Equal";
  }
  return "Not Equal";
}

testStrict(10);

60.Practice comparing different values

// Setup
function compareEquality(a, b) {
  if (a === b) { // Change this line
    return "Equal";
  }
  return "Not Equal";
}

compareEquality(10, "10");

61.Comparison with the Inequality Operator

// Setup
function testNotEqual(val) {
  if (val !=99) { // Change this line
    return "Not Equal";
  }
  return "Equal";
}

testNotEqual(10);

62.Comparison with the Strict Inequality Operator

// Setup
function testStrictNotEqual(val) {
  if (val !==17) { // Change this line
    return "Not Equal";
  }
  return "Equal";
}

testStrictNotEqual(10);

63.Comparison with the Greater Than Operator

function testGreaterThan(val) {
  if (val >100) {  // Change this line
    return "Over 100";
  }

  if (val>10) {  // Change this line
    return "Over 10";
  }

  return "10 or Under";
}

testGreaterThan(10);

64.Comparison with the Greater Than Or Equal To Operator

function testGreaterOrEqual(val) {
  if (val>=20) {  // Change this line
    return "20 or Over";
  }

  if (val>=10) {  // Change this line
    return "10 or Over";
  }

  return "Less than 10";
}

testGreaterOrEqual(10);

65.Comparison with the Less Than Operator

function testLessThan(val) {
  if (val<25) {  // Change this line
    return "Under 25";
  }

  if (val<55) {  // Change this line
    return "Under 55";
  }

  return "55 or Over";
}

testLessThan(10);

66.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 "More Than 24";
}

testLessOrEqual(10);

67.Comparisons with the Logical And Operator

function testLogicalAnd(val) {
  // Only change code below this line

  
  if (val>=25&&val<=50) {
    return "Yes";
  }
  

  // Only change code above this line
  return "No";
}

testLogicalAnd(10);

68.Comparisons with the Logical Or Operator

function testLogicalOr(val) {
  // Only change code below this line

  if (val>20 || val<10) {
    return "Outside";
  }

  // Only change code above this line
  return "Inside";
}

testLogicalOr(15);

69.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;
}

testElse(4);

70.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";}
}

testElseIf(7);

71.Logical Order in If Else Statements

function orderMyLogic(val) {
  if (val < 10 && val>=5) {
    return "Less than 10";
  } else if (val < 5) {
    return "Less than 5";
  } else {
    return "Greater than or equal to 10";
  }
}

orderMyLogic(7);

72.Chaining If Else Statements

function testSize(num) {
  // Only change code below this line
  if(num < 5){
    return "Tiny"
  }else if(num>=5 &&num<10){
    return "Small"
  }else if(num>=10 && num<15){
    return "Medium"
  }else if(num>=15 && num<20){
    return "Large"
  }else{
    return "Huge"
  }
  //return "Change Me";
  // Only change code above this line
}

testSize(7);

73.Golf Code

var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
  // Only change code below this line
  if(strokes==1){
    return "Hole-in-one!";
  }else if(strokes<=(par - 2)){
    return "Eagle";
  }else if(strokes==(par - 1)){
    return "Birdie";
  }else if(strokes==par){
    return "Par";
  }else if(strokes==(par + 1)){
    return "Bogey";
  }else if(strokes<=(par + 2)){
    return "Double Bogey";
  }else{
    return "Go Home!";
  }
  //return "Change Me";
  // Only change code above this line
}

golfScore(5, 4);

74.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";
  }
  // Only change code above this line
  return answer;
}

caseInSwitch(1);

75.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;
}

switchOfStuff(1);

76.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";   
  }
  // Only change code above this line
  return answer;
}

sequentialSizes(1);

77.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";
  }
  // Only change code above this line
  return answer;
}

chainToSwitch(7);

78.Returning Boolean Values from Functions

function isLess(a, b) {
  // Only change code below this line
  return a<b;
  // Only change code above this line
}

isLess(10, 15);

79.Return Early Pattern for Functions

// Setup
function abTest(a, b) {
  // Only change code below this line
  if(a<0 || b<0){
    return
  }
  // Only change code above this line

  return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}

abTest(2,2);

80.Counting Cards (need review)

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++;
    break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
    count--;
}
if(count>0){
  return count+" Bet"
}else{
  return count+" Hold"
}

  return "Change Me";
  // Only change code above this line
}

cc(2); cc(3); cc(7); cc('K'); cc('A');

81.Build JavaScript Objects

var myDog = {
// Only change code below this line
"name":"aa",
"legs":4,
"tails":1,
'friends':["Sam","Susan","David"]
// Only change code above this line
};

82.Accessing Object Properties with Dot Notation

// 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

83.Accessing Object 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

84.Accessing Object 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

85.Updating Object Properties

// Setup
var myDog = {
  "name": "Coder",
  "legs": 4,
  "tails": 1,
  "friends": ["freeCodeCamp Campers"]
};

// Only change code below this line
myDog.name="Happy Coder"

86.Add New Properties to a JavaScript Object

var myDog = {
  "name": "Happy Coder",
  "legs": 4,
  "tails": 1,
  "friends": ["freeCodeCamp Campers"]
};

myDog.bark="woof";

87.Delete Properties from a JavaScript Object

// Setup
var myDog = {
  "name": "Happy Coder",
  "legs": 4,
  "tails": 1,
  "friends": ["freeCodeCamp Campers"],
  "bark": "woof"
};

// Only change code below this line
delete myDog.tails

88.Using Objects for Lookups(need review)

// 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;
}

phoneticLookup("charlie");

89.Testing Objects for Properties

function checkObj(obj, checkProp) {
  // Only change code below this line
  if(obj.hasOwnProperty(checkProp)){
    return obj[checkProp]
  }
  return "Not Found" 
  // Only change code above this line
}

90.Manipulating Complex Objects

var myMusic = [
  {
    "artist": "Billy Joel",
    "title": "Piano Man",
    "release_year": 1973,
    "formats": [
      "CD",
      "8T",
      "LP"
    ],
    "gold": true
  }
];
myMusic.push({
  "artist":"a",
  "title":"b",
  "release_year":1988,
  "formats":["a","b"]
})

91.Accessing Nested Objects

var myStorage = {
  "car": {
    "inside": {
      "glove box": "maps",
      "passenger seat": "crumbs"
     },
    "outside": {
      "trunk": "jack"
    }
  }
};

var gloveBoxContents = myStorage.car.inside["glove box"];

92.Accessing Nested Arrays

var myPlants = [
  {
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
  },
  {
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
    ]
  }
];

var secondTree = myPlants[1].list[1];

93.Record Collection(need review)

// Setup
var collection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

// Only change code below this line
function updateRecords(object, id, prop, value) {
  if(prop!=="tracks" && value!==""){
    object[id][prop]=value;
  }else if(prop==="tracks" && object[id].hasOwnProperty("tracks")===false){
    object[id][prop]=[value];
  }else if(prop==="tracks" && value!==""){
    object[id][prop].push(value);
  }else if(value===""){
    delete object[id][prop]
  }

  return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');

94.Iterate with JavaScript While Loops

// Setup
var myArray = [];
var i=0;
while(i<6){
  myArray.push(i)
  i++;
}
myArray.reverse()

// Only change code below this line

95.Iterate with JavaScript For Loops

// Setup
var myArray = [];
for(var i=1;i<6;i++){
  myArray.push(i)
}
// Only change code below this line

96.Iterate Odd Numbers With a For Loop

// Setup
var myArray = [];

// Only change code below this line
for(var i=1;i<10;i +=2){
  myArray.push(i)
}

97.Count Backwards With a For Loop

// Setup
var myArray = [];

// Only change code below this line
for(var i = 9;i>0;i -= 2){
  myArray.push(i)
}

98.Iterate Through an Array with a For Loop

// Setup
var myArr = [ 2, 3, 4, 5, 6];

// Only change code below this line
var total = 0
for(var i=0;i<myArr.length;i++){
  total += myArr[i]
}

99.Nesting For Loops

function multiplyAll(arr) {
  var product = 1;
  // Only change code below this line
for(let i=0;i<arr.length;i++){
  for (let j=0;j<arr[i].length;j++){
    product *= arr[i][j]
  }
}
  // Only change code above this line
  return product;
}

multiplyAll([[1,2],[3,4],[5,6,7]]);

100.Iterate with JavaScript Do...While Loops

// Setup
var myArray = [];
var i = 10;

// Only change code below this line
do{
  myArray.push(i);
  i++;
}while (i < 5)

101.Replace Loops using Recursion

function sum(arr, n) {
  // Only change code below this line
  if(n<=0){
    return n
  }else{
    return sum(arr,n-1)+arr[n-1]
  }
  // Only change code above this line
}

102.Profile Lookup(need review)

// Setup
var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    },
    {
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["JavaScript", "Gaming", "Foxes"]
    }
];


function lookUpProfile(name, prop){
// Only change code below this line
for(let i =0;i<contacts.length;i++){
    if(contacts[i]["firstName"]===name){
        if(contacts[i].hasOwnProperty(prop)){
            return contacts[i][prop]
        }else{
            return "No such property"
        }
    }    
}
return "No such contact"

// Only change code above this line
}

lookUpProfile("Akira", "likes");

103.Generate Random Fractions with JavaScript

function randomFraction() {

  // Only change code below this line

  return Math.random();

  // Only change code above this line
}

104.Generate Random Whole Numbers with JavaScript

function randomWholeNum() {

  // Only change code below this line

  return Math.floor(Math.random()*10);
}

105.Generate Random Whole Numbers within a Range

function randomRange(myMin, myMax) {
  // Only change code below this line
  return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
  // Only change code above this line
}

106.Use the parseInt Function

function convertToInteger(str) {
  var a = parseInt(str)
  return a
}

convertToInteger("56");

107.Use the parseInt Function with a Radix

function convertToInteger(str) {
  var a=parseInt(str,2)
  return a
}

convertToInteger("10011");

108.Use the Conditional (Ternary) Operator

function checkEqual(a, b) {
  return a == b ? "Equal":"Not Equal"
}

checkEqual(1, 2);

109.Use Multiple Conditional (Ternary) Operators

function checkSign(num) {
  return (num===0) ? "zero" : (num>0) ? "positive" : "negative";
}

checkSign(10);

110.Use Recursion to Create a Countdown

// Only change code below this line
function countdown(n){
  return n < 1 ? [] : [n].concat(countdown(n - 1));
}
// Only change code above this line

111.Use Recursion to Create a Range of Numbers

function rangeOfNumbers(startNum, endNum) {
  return startNum===endNum ? [startNum] : rangeOfNumbers(startNum,endNum-1).concat(endNum);
};
 

  • 16
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值