Week 3 (Beginning Jan 24th) Learning Week Cycle 3 - emcsquared2/2022-Learning GitHub Wiki
Week 3 (Beginning Jan 24th) Learning Week Cycle 4
GCSE Computer Science
The Use of SQL to search for Data
Takeaway learning
SQL is used to create, delete, modify and manipulate records in a database The basic commands include:
- SELECT which fields to be returned. * Can be used for all fields.
- FROM which table
- WHERE records meet a condition
MIMO mobile app
#####Methods in Classes
Takeaway learning
Adding a method in a class is like creating a regular function without the word "function"
class VirtualPet{
constructor(name) {
this.name = name
}
eat() {
console.log("nom nom");
}
}
//create a new object
var pet = new VirtualPet("Tom");
pet.eat();
//OUTPUT "nom nom"
#####Instances
Takeaway learning
Every time we create an object from a class we are creating an "instance" of that class.
Professor Steve
Playing with Objects
Takeaway learning
Objects are lists like arrays except instead of being numbered they have labels. Revision of object properties
Codewars Exercise (Level 8)
In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?
My Solution
const makeNegative = (num) => num>0 ? -num: num;
I did see another solution but I'm not sure about it because it returns a negative 0 value. Still it is a good learning point around the Absolute value Math function.
function makeNegative(num) {
return -Math.abs(num);
}
JS Practice
Given a string of digits, output all the contiguous substrings of length n
in
that string in the order that they appear.
My solution
The solution required a particular structure to this answer in the form of a Class. The Class creates an object with each slice of numbers returned as numbers within an array. To start with I just worked out how to simply do this by returning a string in a simple function...
str = "123456"
n = 3;
for (let i=0; i<=str.length-n; i++){
return str.substring(i,n+i)
};
That worked but wasn't what would pass the tests which also required some constraints on the inputs. I had to go to the community to get a solution to help. Here is my solution in the end:
class Series {
constructor(series) {
this.series = series;
}
slices(sliceLength) {
if (!this.series) throw new Error('series cannot be empty');
if (sliceLength > this.series.length) throw new Error ('slice length cannot be greater than series length');
if (sliceLength === 0) throw new Error ('slice length cannot be zero')
if (sliceLength < 0) throw new Error ('slice length cannot be negative')
const slices = []
for (let i=0; i<=this.series.length-sliceLength; i++){
slices.push((this.series.substring(i,sliceLength+i)).split('').map(Number))
};
return slices;
};
};
const series1 = new Series('12345')
console.log(series1)
console.log(series1.slices(3))
//OUTPUT [ [ 1, 2, 3 ], [ 2, 3, 4 ], [ 3, 4, 5 ] ]
Udacity (Object Orientated JavaScript - Free Course)
Object.keys() and Object.values() Methods
Takeaway learning
So to start with I wanted to see if I could freestyle using a for loop and get the keys and values:
const mealPlanner = {
breakfast: "cereal",
lunch: "sandwich",
dinner:"fish and chips"
};
const keyArray = [];
for (key in mealPlanner){
keyArray.push(key);
};
console.log(keyArray);
//OUPUT [ 'breakfast', 'lunch', 'dinner' ]
const valueArray = [];
for (key in mealPlanner){
valueArray.push(mealPlanner[key])
};
console.log(valueArray);
//OUTPUT [ 'cereal', 'sandwich', 'fish and chips' ]
I'm really pleased that I can write these bits of code now with no trouble at all and correct myself as I go. The equivalent using the Object Method is...
console.log(Object.keys(mealPlanner));
console.log(Object.values(mealPlanner));
Code Signal
Given a divisor and a bound, find the largest integer N such that: N is divisible by divisor. N is less than or equal to bound. N is greater than 0.
My Solutions
function solution(divisor, bound) {
let int = 0;
for (let i=bound; i>1; i--){
if (i%divisor===0){
int = i;
break;
};
};
return int;
};
I've realised I have done a very convoluted way of solving this!! A much simpler version...
function solution(divisor, bound) {
return bound - (bound % divisor);
}