Skip to content

Code Learning freeCodeCamp

davidvanr21 edited this page Nov 19, 2020 · 1 revision

Today I'm going to exercise the basics of JavaScript on freeCodeCamp

You can easily increment or add one to a variable with the ++ operator.

i++;

is the equivalent of

i = i + 1;

var myVar = 87;

// Only change code below this line

myVar++;

result = 88

myVar—;

Result 86

Zero-based indexing [0]

multi-dimensional array > object1, 13], [object2, 26

.push() takes one or more parameters and "pushes" them onto the end of the array.

// Setup
var myArray = [["John", 23], ["cat", 2]];

// Only change code below this line
myArray.push(["dog", 3])

.unshift aadds the eleent at the beginning of the array

.pop() is used to "pop" a value off of the end of an array. We can store this "popped off" value by assigning it to a variable. In other words, .pop() removes the last element from an array and returns that element.

pop() always removes the last element of an array. What if you want to remove the first?

That's where .shift() comes in. It works just like .pop(), except it removes the first element instead of the last.

You can call or invoke this function

Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or "passed") into a function when it is called are known as arguments.

Here is a function with two parameters, param1 and param2:

function testFun(param1, param2) {
  console.log(param1, param2);
}

Then we can call testFun: testFun("Hello", "World"); We have passed two arguments, "Hello" and "World". Inside the function, param1 will equal "Hello" and param2 will equal "World". Note that you could call testFun again with different arguments and the parameters would take on the value of the new arguments.

functionWithArgs(1, 2)

function functionWithArgs(param1, param2){
    console.log(param1 + param2);
}

Console print looks like: 3

Different kinds of variables:

var, const, let

Var: Variables which are used without the var keyword are automatically created in the global scope.

Global variable inside a function

Argument roept parameter aan

== zegt dat 3 en '3' true zijn

=== zeg dat 3 en 3 true zijn

! = not equal

! ==

= Equal or greather than

pseudocode = 'Pseudo' betekent onecht, 'code' verwijst naar de broncode van een computerprogramma. Doordat pseudocode een informeel karakter heeft, is het niet geschikt om gecompileerd en uitgevoerd te worden.

Give values to an empty string in a variable with switch, case and break

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

  switch (val){
    case 1:
      answer = "alpha";
      break;
    case 2:
      answer = "beta";
      break;
    case 3:
      answer = "gamma";
      break;
    case 4:
      answer = "delta";
      break;
    }

  return answer;
}

console.log(caseInSwitch(1));

When you don't use the break code, the print will always be delte because the console will read that one last.

function sequentialSizes(val) {
  var answer = "";
  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;
  }
  return answer;
}

console.log(sequentialSizes(2));

The following code can be made easier:

function isEqual(a,b) {
  if (a === b) {
    return true;
  } else {
    return false;
  }
}
function isEqual(a,b) {
  return a === b;
}

Because we use booleans, the return is false or true anyway. This is why we dont need to write this out with if else.

This was an example from freeCodeCamp. The code they give me to clean up was the following code:

function isLess(a, b) {
  if (a < b) {
    return true;
  } else {
    return false;
  }
}

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

console.log(isLess(8, 15)); //outcome is true
console.log(isLess(17, 15)); //outcome is false

Objects are similar to arrays, except that instead of using indexes to access and modify their data, you access the data in objects through what are called properties.

var cat = {
  "name": "Whiskers",
  "legs": 4,
  "tails": 1,
  "enemies": ["Water", "Dogs"]
};

There are two ways to access the properties of an object: dot notation (.) and bracket notation ([]), similar to an array.

var myObj = {
  prop1: "val1",
  prop2: "val2"
};
var prop1val = myObj.prop1; // val1
var prop2val = myObj.prop2; // val2
var testObj = {
  12: "Namath",
  16: "Montana",
  19: "Unitas"
};

var playerNumber = 16;      
var player = testObj[playerNumber];

console.log(playerNumber); // 16
console.log(player); // Montana

Add properties by just typisch myDog.bark = "...". delete properties with delete myDog.bark

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

    var lookup = {
      "alpha": "Adams",
      "bravo": "Boston",
      "charlie": "Chicago",
      "delta": "Denver",
      "echo": "Easy",
      "foxtrot": "Frank",
    }

    result = lookup[val]

  return result;
}

console.log(phoneticLookup("bravo"));

Feedback repo's algemeen:

  • Veel onderzoeksvragen over elektrische auto's, liever wat originelers doen
  • Concepten en schetsen heb ik nog niet. Niet naar standaard kaarten als visualisaties doen. Lekker ontwerpen!!!
  • Zet bij API goed wat voor type data eruit komt
  • Engels correct typen.

Feedback Sam

Clone this wiki locally