loading...

This Week I Learned - Wk2

snesjhon profile image Jhon Paredes ・5 min read

This is an ongoing blog about my journey in improving my javascript, algorithm, react skills for future interviews


Sep 1, 2020

Recap

  • To analyze the performance of an algorithm we use BigO notation
  • BigO notation can give us a high level understanding of the time or space complexity of an algorithm
  • BigO doesn’t care about precision, only about general trends
  • The time or space complexity (measured by BigO) depends only on the algorithm, not the hardware used to run the algorithm

Logarithms

  • what is a logarithm?
    • Logarithm is the inverse of exponentiation
    • log[base](value) = exponent
    • the logarithm of a number roughly measures the number of times you can divide that number by 2 before you get a value that’s less than or equal to one

Space Complexity

O(1)

  • When we don’t take anymore space as we go through the loop, we’re not adding to the array, but rather replacing that into the variable declaration
function sum(arr){
    let total = 0; 
    for(let i = 0; i < arr.length; i++){
        total += arr[i];
    }
    return total;
} 

O(n)

  • In terms of space complexity that increases based on the length of the array. That means that the array, for every push, will increase based on what’s passed into the function
  • Since we’re passing an array the then pushing into this array that means we're growing based on what’s in the array that we’re passing
function double(arr){
    let newArr = [];
    for(let i = 0; i < arr.length; i++){
        newArr.push(2 * arr[i);
    } 
    return newArr;
} 

Sep 3, 2020

JustJavaScript

  • Values
    • You can’t pass a variable. Rather you can pass the current value of the variable
    • That means in a function we’re not actually passing the variable, but rather the value that is of that expression.
function double(x) {
 x = x * 2;
}

let money = 10;
double(money);
console.log(money);
  • Rather than thinking about it in that we’re passing money and then doubling it, we’re rather passing double(10) and then figuring out what these values equate to within the function. That allows for the local scope of the function to not leak out.

  • What is the Mental Model of

let x = 10;
let y = x;
x = 0; 

console.log(y) //? 
* x -> 10 (x points to 10 as a value)
* `let y = x `  x -> 10, as this is expression evaluates to 10 so we’re assigning this variables to 10
* `x = 0` x -> 0 : we’re reassigning the `x` to be of 0, but we’re not doing the same towards the `y` so it stays the same and doesn't get reassigned. 
  • Variables don’t POINT to other variable, but rather they point to VALUES. And in the mental model above we’re pointing to the VALUE or 10. So that means that the value that we’re assigning would still be kept unless we change it directly —
let pet = Narwal;
pet = The Kraken;
console.log(pet);
  • This will be the kraken that’s because you’re overwriting the variable which you ARE in control of, and not the primitive.
let reaction = yikes
reaction[0] = l
console.log(reaction) 
  • reaction is a string, which isn’t an array. But I also know that there's a getCharAt that also allows for this to happen. So in that case does that mean that we’d be able to change it based on an index?
  • I think it’d just output yikes because the reaction[0] wouldn't map to another place?
  • Can you map a string? I know you can split a string. Well in that case that means that the string can be changed?
  • Answer: -> you can’t change primitive values -> Depending on strict mode it’ll either print an error or just yikes you can't change the string.

JavaScript

Type Coercision

  • Type coercion is the automatic or implicit conversion of values from one data type to another.
  • Type coercion is implicit whereas type conversion can either be implicit or explicit

Problem Solving

  • Can I restate the problem in my own words?
  • What are the inputs that go into the problem
  • What are the outputs that should come from the solution to the problem?
  • Can the outputs be determined from the inputs?
    • Do I have enough information to solve the problem?
  • How should I label the important pieces of data that are a part of the problem?

Sep 4, 2020

JustJavaScript

Different types of Values

  • undefined
    • This can be thought of as a black hole
    • undefined is a value and a very real one.
    • In JavaScript, it represent the concept of unintentionally missing value
    • undefined is a regular primitive
  • null
  • boolean
  • numbers
    • floating point math
      • JavaScript treats numbers as having a limited precision
      • There are only 18 quintillion of them

Sep 5, 2020

Values Types

[image:AA71609B-1164-4B3C-A541-3E4C69C55797-54883-00013479A181E0D2/95061682-1E2F-4387-9CC5-87C6E2924BAB.png]

Primitives

  • Undefined: Only one value, undefined.
  • Null: Only one value, null.
  • Booleans: Two values: true and false.
  • Numbers: One value for each floating point math number.
  • BigInts: One value for every conceivable integer.
  • Strings: One value for every conceivable string.
  • Symbols: We skipped Symbols for now, but we’ll get to them someday!

Functions and Object

  • These types are special because they let us make our own values:
  • Objects: One value for every object literal we execute.
  • Function: One value for every function expression we execute.

JustJavaScript

Functions

  • Using () changes the meaning of our value.
let countD = function() { return 7; };
let d = countD(); // () is a function call
console.log(d);
  • let d = countD means “Point d toward the value that countD is pointing to
  • let d = countD() means “Point d towards the value RETURNED BY the function that countD is pointing to.

  • Function are like values that we can evaluate in an expression, but they’re technically are objects, but they have other special properties.

  • Unlike objects, do we create new function every time we “make” them.

  • Every time we execute a line of code that contains a function expression, a brand new function value appears in our universe.

Objects

  • Going along with what was talked about in strings. We don’t “create” or “make” a string, but rather we “summon” a string of something that’s already been created. All the numbers, string, they’re already existing, so we can point to it.
  • But with Objects, we’re actually “creating” a new object.
  • Every time we use a {} object literal, we create a brand new object value.
  • We can create objects, but we cannot destroy them. Since JavaScript is a garbage collected language we’re not concerned with how we get rid of these objects.

BigInts

  • Conceptually these are numbers outside of the boundaries of regular numbers that means that they’re of the right precision when they need to be.

Strings

  • STRINGS ARE NOT OBJECTS
  • They do have properties though:
let cat = "jhon"
console.log(cat.length); // 4
console.log(cat[0]); // "j"
  • This might seem like a weird thing to call out, but overall the idea that strings are of a primitive type BUT they have properties attached to them can be confusion.
  • All conceivable string values already exist from the beginning — one value for every distinct string.

Discussion

pic
Editor guide