Skip to content

Instantly share code, notes, and snippets.

View sj-ransom's full-sized avatar

Stephanie Ransom sj-ransom

View GitHub Profile
Object creator
function createMyObject(key1, key2, key3, key4) {
obj = {
foo: 'bar',
answerToUniverse: 42,
'olly olly': 'oxen free',
sayHello: function sayHello() {
return `hello`
}
What is scope? Your explanation should include the idea of global vs. local scope.
Scope refers to the boundaries in which a variable or constant will interact with functions in code.
A variable has global scope when it is defined outside of a function. Global variables will be recognized
by functions throughout a folder of JS files.
A variable has local scope when it is defined within a function. A local variable is only recognized
within its function.
Why are global variables avoided?
As an app becomes more complex and requires many functions, global variables can cause there to be bugs
which are difficult to find. For example, if a global variable is defined in one JS file and altered in a function in another JS File in another,
max and min (without sort)
function max(numbers) {
maxNumber = numbers[0];
for (let i = 0; i < numbers.length; i++) {
if (maxNumber < numbers[i]) {
maxNumber = numbers[i];
}
}
return maxNumber
Array copying I
function firstFourItems(array) {
return array.slice(0, 4)
}
function lastThreeItems(array) {
return array.slice(-3)
}
Creating arrays
function makeList(item1, item2, item3) {
return [item1, item2, item3]
}
Adding array items
function addToList(list, item) {
list.push(item);
function doTrafficLights() {
const activeLight = getActiveLight();
if (activeLight === 'red') {
turnRed()
} else if (activeLight ==="green") {
turnGreen()
} else {
turnYellow()
}
}
function computeArea(width, height) {
return width * height;
}
function celsToFahr(celsTemp) {
return celsTemp * 1.8 + 32;
}
function fahrToCels(fahrTemp) {
return (fahrTemp - 32) * 1.8;
function textNormalizer(text) {
return `${text.toLowerCase().trim()}`
}
function shouter(whatToShout) {
return `${whatToShout.toUpperCase()}!!!`;
}
function wisePerson(wiseType, whatToSay) {
return `A wise ${wiseType} once said: "${whatToSay}".`;