top of page
Search

JavaScript code

  • Writer: Keshav Batra
    Keshav Batra
  • Aug 4, 2022
  • 6 min read

I'm about halfway done with the lesson and I've commented on a lot of the lines so that I would be able to remember the steps of what I've done. I've learned how to use arrays and notations by using the number at the bottom to calculate whether the value is greater than or equal to the number given. Below are some examples of what I have done.




function testLessThanOrEqual(val){
    if(val <= 12){ //10 <=12 is true so it will print "Smaller Than or Equal to 12"
        return "Smaller Than or Equal to 12";
    }

    if(val <= 24){ //10 <= 24 is true
        return "Smaller Than or Equal to 24";
    }

    return "More Than 24";
}

console.log(testLessThanOrEqual(10)); //Smaller than or equal to 12

//val = 10

//And/Or Operators

function testLogicalAnd(val){ //Both statements need to be true to return yes
    if(val <= 50 && val >= 25) { //10 <= 50 is true but 10 >= 25 is false so it will return no
        return "Yes";
    }

    return "No";
}

testLogicalAnd(10);

function testLogicalOr(val){

    if (val < 10 || val > 20) { // || means or
        //15 < 10 and 15 > 20 are both false so they will return inside
        return "Outside"; 
    }

     return "Inside";
}

testLogicalOr(15); 

//Else Statements

function testElse(val){
    var result = "";

    if (val > 5) {
        result = "Bigger than 5";
    } else { //If the result is not bigger than 5 it will return 5 or smaller
        result = "5 or smaller";
    }
    
    return result;
}

testElse(4); //The val is 4

//Else if statements

function testElseIf(val){ //testElseIf(7)
    if(val > 10) { // 7 > 10 is false therefore it will not print
        return "Greater than 10";
    } else if(val < 5) {  //7 < 5 is false therefore it will not print
        return "Smaller than 5";
    } else {
        return "Between 5 and 10";
    }
}

testElseIf(7); //7 is the val

//Logical Order

function orderMyLogic(val){

    if(val < 5) { //3 < 5 is true therefore it will print
        return "Less than 5";
    } else if (val < 10) { //3 < 10 is true which will print less than 10
        return "Less than 10";
    } else {
        return "Greater than or equal to 10"; //If the if statement is false then the else if statement will run and if thats false then the else statement will run 
    }
}

console.log(orderMyLogic(3)); //Less than 5

//Val is 3

//Chaining If Else Statements

function testSize(num) { //TestSize is a function and num is a parameter
  if (num < 5) {  // 20 < 5 which is false
    return "Tiny"
  } else if (num < 10) { //20 < 10 which is false
    return "Small"
} else if (num < 15) { // 20 < 15 is false
    return "Medium"
} else if (num <= 20) { // 20 <= 20 is true 
    return "Large"
} else {
    return "Huge"
}
}

console.log(testSize(20)); //Large
//The function is being called in the console and is being passed a value of 20
//Num = 20
//Golf Code

var names = ["Hole-in-one", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Home"] //The number always starts at 0 and will go by the position of the array 
function golfScore(par, strokes)  { //This represents the position of the parameters

}
if(strokes == 1) {  //4==1 which is false
    return names[0] //Hole-in-one
} else if(strokes <= par - 2) { //If 4 is less than or equal to 5 - 2 which equals 3, its false since 4 would be greater than 3
    return names[1] //Eagle
} else if (strokes == par - 1) { //4 == 5-1 which equals 4 therefore making it true
    return names[2] //Birdie
} else if (strokes == par) { //4==5 which is false
    return names[3] //Par
} else if (strokes == par + 1) { //4 == 5 + 1 which equals 6 therefore making it false
    return names[4] //Bogey 
}  else if (strokes == par + 2) { //4 == 5 + 2 which equals 7 making it false unless its changed to less than or equal to 7
    return names[5] //Double Bogey 
}  else if (strokes >= par + 3) { //4 >= 5 + 3 which comes out to 8 making it false unless its changed to less than 8
    return names[6] //Home
}

console.log(golfScore(5, 4)); //Birdie 

//The 5 represents the par and 4 represents the strokes

//Switch Statements

function caseInSwitch(val){ //In this case val = 1 which is "alpha"
    var answer = ""; //The answer is the name of the variable in this case
    switch(val) { //The represents the number that is being passed in which case its 1
     case 1: //Case is compared to the value thats passed
        answer = "alpha"; //1 will display as alpha
        break;
     case 2:
        answer = "beta" //2 will display as beta
        break;
     case 3:
        answer = "gamma" //3 will display as gamma
        break;
     case 4:
        answer = "delta" //4 will display as delta
        break;
    }

    return answer;
}

console.log(caseInSwitch(1)); //Function is called and a value is passed

/*
Write a switch statement which tests val and sets answer for the following conditions:
1 - "alpha"
2 - "beta"
3 - "gamma"
4 - "delta"
*/

//Default option//

function switchOfStuff(val){
    var answer = "";
    switch (val) {
        case "a": //Displays "apple"
          answer = "apple" 
          break;
        case "b": //Displays "bird"
          answer = "bird"
          break;
        case "c": //Displays "cat"
          answer = "cat"
          break;
        default: //used for anything else that will be passed through
        answer = "stuff"; //This will display in place of a, b, and c regardless of the number in the switchOfStuff
        break;
    }
    return answer;
    }

    console.log(switchOfStuff("c")); //This will display "cat"

    //Multiple Identical Options

    function sequentialSizes(val) {
        var answer = "";
        switch(val) {
            case 1:
            case 2:
            case 3:
                answer = "Low"; //Will display "Low" if 1,2 or 3 are through
                break; //Breaks are used to separate the groups of code
            case 4:
            case 5:
            case 6:
                answer = "Mid"; //Will display "Mid" if 4,5, or 6 are through
                break;
            case 7:
            case 8:
            case 9:
                answer = "High"; //Will display "High" if 7,8, or 9 are passed through
                break;
        }
        return answer;
    }

    console.log(sequentialSizes(1)); //This will display "Low" in the console since 1 is passed

    //Replacing If Else Chains with Switch

function chainToSwitch(val){
    var answer = "";

    if(val == "bob") {
        answer = "Marley";
    } else if (val === 42) {
        answer = "The Answer";
    } else if (val === 1) {
        answer = "There is no #1";
    } else if (val === 99) {
        answer = "Missed me by this much!"
    } else if (val === 7) {
        answer = "Ate Nine";
    }

    return answer;

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

}

//Returning Boolean Values from Functions

function isLess(a, b) { // a = 10, b = 15
 if (a < b) { //since it shows 15 < 10 it will go through as false

    return true;
 } else {
    return false;
 }
}

isLess(10,15);

function isLess(a, b) {
    return (a < b) 
}
   console.log (isLess(20,15));

// Returning Early Pattern from functions

   function abTest(a, b){ //

    if(a < 0 || b < 0){
        return undefined
    }

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

  console.log(abTest(2,2));

  //Counting Cards

  var count = 0;

  function cc(card) {
    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--;
            break;
    }

    var holdbet = "Hold"
    if(count > 0){
        holdbet = 'Bet'
    }
    return count + " " + holdbet;
  }

  cc(2); cc(3); cc(7); cc('K'); cc('A');
  console.log(cc(4))

  //Build Objects

  var ourDog = {

    "name": "Camper",
    "legs": 4,
    "tails": 1,
    "friends": ["everything!"]
  };

  var myDog = {

    "name": "Quincy",
    "legs": 3,
    "tails": 2,
    "friends": []
  }

  //Dot Notation

  var testObj = {
    "hat": "ballcap", //This will be the hatValue
    "shirt": "jersey", //This will be the shirtValue
    "shoes": "cleats", //This will be the shoe value
  };

  var hatValue = testObj.hat; // testObj.hat = ballcap
  var shirtValue = testObj.shirt; // testObj.hat = jersey
  var shoeValue = testObj.shoes; // testObj.hat = cleats

  //Bracket Notation

  var testObj = {
    "an entre": "hamburger", // This will be the entreeValue
    "my side": "veggies", // This will be the sideValue
    "the drink": "water" // This will be the drinkValue
  };

  var entreeValue = testObj["an entree"]; // entreeValue == "hamburger"
  var drinkValue = testObj['the drink']; // drinkValue = "water"
  var sideValue = testObj["my side"] // sideValue = "veggies"





 
 
 

コメント


bottom of page