Skip to content

Instantly share code, notes, and snippets.

View liseferguson's full-sized avatar

Lise liseferguson

  • Portland, OR.
View GitHub Profile
1.) Make Student Reports
function makeStudentsReport(data) {
const results = [];
for (let i = 0; i < data.length; i++) {
const item = data[i];
results.push(`${item.name}: ${item.grade}`);
}
return results;
}
@liseferguson
liseferguson / text
Created March 21, 2018 23:21
Objects drills
1.) Object creator drill
function createMyObject() {
return {
foo: "bar",
answerToUniverse: 42,
"olly olly": "oxen free",
sayHello: function() {
return "hello";
}
@liseferguson
liseferguson / gist:3f3e6fae077c069c11915ffe34695356
Created March 21, 2018 19:43
Challenge: In your own words
1.) What is scope? Your explanation should include the idea of global vs. local scope.
Scope is the extent of the area in the code where a piece of code can behave or be accessed. How the code is executed depends on where the variables are defined within the scope chain. Global scope is when a variable is defined outside of a function. This means it can be used anywhere in the code, therefore it is global. Local scope is when a variable is defined inside of the function, and can therefore only be executed within that code's block of instructions. Outside of the function it is defined inside, it ceases to exist, and the code will try to access variables in the parent code, and eventaully the global scope.
2.) Why are global variables avoided?
Global variables can be accessed anywhere in the code and even across files, but they can also be altered in unintended ways. If the code has undefined variables, for instance, it will search through the scope chain until it finds a variable to use, and sicne the glo
function fizzBuzz(countTo) {
const result = [];
for (let i = 1; i<=countTo; i++) {
if (i % 15 === 0) {
result.push("fizzbuzz");
} else if (i % 5 === 0) {
result.push("buzz");
} else if (i % 3 === 0) {
result.push("fizz");
} else {
function average(numbers) {
let total = numbers[0];
for (let i = 1; i < numbers.length; i++) {
total += numbers[i];
}
return total / numbers.length;
}
/* From here down, you are not expected to
understand.... for now :)
function max(numbers) {
let currentMax = numbers[0];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > currentMax) {
currentMax = numbers[i];
}
}
return currentMax;
}
@liseferguson
liseferguson / index.js
Created March 15, 2018 03:06
find drill
function divisibleBy5(array) {
return array.find(function(num) {
return num % 5 === 0;
});
}
/* From here down, you are not expected to
understand.... for now :)
@liseferguson
liseferguson / index.js
Created March 15, 2018 01:07
Sorting - greatest to least
function greatestToLeast(array) {
return array.sort(function(a, b) {return b-a;
});
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
@liseferguson
liseferguson / index.js
Created March 15, 2018 00:46
Array copying 2
function minusLastItem(array) {
return array.slice(0, array.length-1);
}
function copyFirstHalf(array) {
return array.slice(0, array.length / 2);
}
/* From here down, you are not expected to
understand.... for now :)
@liseferguson
liseferguson / index.js
Created March 15, 2018 00:31
array copying 1
function firstFourItems(array) {
return array.slice(0, 4);
}
function lastThreeItems(array) {
return array.slice(-3);
}
/* From here down, you are not expected to
understand.... for now :)