Skip to content

Instantly share code, notes, and snippets.

View jamessergeant's full-sized avatar

James Sergeant jamessergeant

View GitHub Profile
// Cat carousel
$(function() {
$('.thumbnail img').click(function(event) {
$('.hero img').attr('src',$(this).attr('src'));
})
});
// Fizzbuzz (should probably break down to separate functions as in reference solution)
// discussed when used in mostFrequentWord
function getTokens(rawString) {
// NB: `.filter(Boolean)` removes any falsy items from an array
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
}
function mostFrequentWord(text) {
var words = getTokens(text); // splits text by spaces and punctuation into array of words (strings), removes empty strings and sorts
var wordFrequencies = {}; // declare variable and initialise with empty object, this is used as a frequency counter
for (var i = 0; i <= words.length; i++) { // loop over words
function makeStudentsReport(data) {
return data.map(function(student) {
return `${student.name}: ${student.grade}`
})
}
function enrollInSummerSchool(students) {
return students.map(function(student) {
return {
name: student.name,
function createMyObject() {
var obj = {
foo: 'bar',
answerToUniverse: 42,
'olly olly': 'oxen free',
sayHello: function() {
return 'hello';
}
}
return obj

What is scope? Your explanation should include the idea of global vs. local scope.

Scope determines where a variable can be accessed. A variable with local scope can only be accessed within that function. A variable with global scope can be accessed anywhere unless it's variable name is shadowed by a local variable.

Why are global variables avoided?

To avoid unexpected or indeterminate behaviour.

Explain JavaScript's strict mode

Strict mode forces a variety of conventions including the use of the var keyword when declaring variables.

What are side effects, and what is a pure function?

function max(numbers) {
var max = -Infinity;
for (i=0; i < numbers.length; i++) {
max = max > numbers[i] ? max : numbers[i];
}
return max
}
function min(numbers) {
function makeList(item1, item2, item3) {
return [item1, item2, item3];
}
function addToList(list, item) {
list.push(item);
return list;
}
function accessFirstItem(array) {
function doTrafficLights() {
var activeLight = getActiveLight();
if (activeLight === 'red') {
turnRed();
} else if (activeLight === 'yellow') {
turnYellow();
} else if (activeLight === 'green') {
turnGreen();
}
}
@jamessergeant
jamessergeant / drills-2.2.5.js
Created August 9, 2017 10:15
Drills for Project 2.2.5 of Fundamentals of Web Development
function computeArea(width, height) {
return width * height;
}
function celsToFahr(celsTemp) {
return celsTemp * 9 / 5 + 32;
}
function fahrToCels(fahrTemp) {
return (fahrTemp - 32) * 5 / 9;
@jamessergeant
jamessergeant / drills.js
Created August 9, 2017 10:01
Snippets of code for JS drills for Funcdamentals of Web Development - Project 2.2.3
// Would recommend finding a way to hide the test function...
function wisePerson(wiseType, whatToSay) {
return 'A wise ' + wiseType + ' once said: "' +
whatToSay + '".';
}
function shouter(whatToShout) {
return whatToShout.toUpperCase() + '!!!';
}