Week 1 (Beginning Jan 10th) Learning Week Cycle 1 - emcsquared2/2022-Learning GitHub Wiki

Week 1 (Beginning Jan 10th) Learning Week Cycle 1

GCSE Computer Science

https://www.youtube.com/watch?v=oQAQNKomako&list=PLCiOXwirraUAEhj4TUjMxYm4593B2dUPF&index=68 Data Types and Casting

Takeaway learning

Different data types: Integers, Real(float), Character, String, Boolean Integers take up less memory than decimals Casting a character means converting it to an integer. When you hit a number on the keyboard it needs to be converted to an integer (casting)


Professor Steve

https://www.youtube.com/watch?v=OLXyquuM05Q&list=PLyuRouwmQCjkYdv4VjuIbvcMZVWSdOm58&index=6 Logical operators AND and OR with compound if statements

Takeaway learning

I was already confindent with logical operators and if statements but had to think a bit about how he created the function first and got the function to return true or false in the for loop if the parameter matched the array element. To practice this I created my own:


const smoothieHas = (fruits) => {
    for(let i of smoothieFruits){
        if(i==fruits){
            return true;
        } 
    }
    return false;
}
if (smoothieHas('kale') || smoothieHas('spinach')){
    console.log('contains veg')  
} else {
    console.log('no veg')
};

Codewars Exercise (Level 8)

Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.

My Solution

const even_or_odd = num => num%2===0 ? "Even":"Odd";

I found this very easy


JS Practice

Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the modulo operation.

The rules of raindrops are that if a given number:

- has 3 as a factor, add 'Pling' to the result. - has 5 as a factor, add 'Plang' to the result. - has 7 as a factor, add 'Plong' to the result. - does not have any of 3, 5, or 7 as a factor, the result should be the digits of the number.

My solution

export const convert = (num) => {
let str = '';
 
str += num%3===0 ? 'Pling': '';
str += num%5===0 ? 'Plang': '';
str += num%7===0 ? 'Plong': '';
return str === ''? num.toString() : str;

throw new Error('Remove this statement and implement this function');
};

I completed this task successfully but I'm still unsure about what the throw new Error bit means. I think it's just part of how it's tested in npm. I had a look at community solutions and some seemed quite challenging to understand. I know that I need more practice on array methods and objects. I will practice these more over time using different resources.


Udacity (Object Orientated JavaScript - Free Course)

Takeaway learning

There are two ways to create objects in JS

// Using literal notation:

const myObject = {};

// Using the Object() constructor function:

const myObject = new Object();

Objects in JavaScript are passed by reference, if we make changes to that reference, we're actually directly modifying the original object itself

let originalObject = {
  favoriteColor: 'red'
};

function setToBlue(object) {
  object.favoriteColor = 'blue';
}

setToBlue(originalObject);

originalObject.favoriteColor;
// 'blue'

Whereas if you just pass a primitive data type into a function, it creates a copy and not a reference...

function changeToEight(n) {
  n = 8; // whatever n was, it is now 8... but only in this function!
}

let n = 7;

changeToEight(n);

console.log(n);
// 7

The primitive is not affected outside of the function. Hence the output is 7 due to the global variable setting n to 7.

In the same way if you set assign an object to a new variable e.g. objectCopy = objectOriginal any changes you make to the copy will also affect the original.

Another example when

let string = 'orange';

function changeToApple(string) {
  string = 'apple';
}

changeToApple(string);

console.log(string);
// OUTPUT 'orange'

Any changes made to it inside a function effectively creates a copy local to that function, and does not affect the primitive outside of that function (immutable) The opposite is true for properties within an object....

const oven = {
  type: 'clay',
  temperature: 400
};
const newOven = oven;

newOven.temperature += 50;

This will change the mutable object property of the oven object to 450.

Comparing Objects

To identical objects stored in different variables e.g. pigeon and parrot pigeon === parrot //returns FALSE This is because the expression will only return true when comparing two references to exactly the same object. i.e. pigeon = myBird

pigeon === myBird //returns TRUE


Code Signal

You are given a two-digit integer n. Return the sum of its digits.

My Solutions


//For an accumulation of any number of digits)
function solution(n) {
    return n.toString().split('').reduce((accum, curr)=> +accum + +curr, 0)
    }

//I can do this just using a loop instead of the array method...
function solution(n){
    let sum = 0;
    arr = n.toString().split('')
    for (let i=0; i < arr.length; i++){
        sum+= +arr[i]
    }
    return sum
}

//For 2 digits only
function solution(n) {
        return +n.toString()[0] + +n.toString()[1] 
        }

//Community solution.  V.clever!

function solution(n) {
    return n%10 + Math.floor(n/10)
}