Skip to content

Instantly share code, notes, and snippets.

//WisePerson generator
function wisePerson(wiseType, whatToSay) {
return ("A wise " + wiseType + " once said: \"" +
whatToSay + "\".");
console.log(phrase);
}
//Shouter
function shouter(whatToShout) {
return whatToShout.toUpperCase() + "!!!";
//Area
function computeArea(width, height) {
return width * height;
}
//Temp conversion
function celsToFahr(celsTemp) {
return (celsTemp * 9)/5 + 32;
}
//Traffic Light
function doTrafficLights() {
var activeLight = getActiveLight();
if (activeLight === "red"){
turnRed();}
else if (activeLight === "yellow"){
turnYellow();}
else if (activeLight === "green"){
turnGreen();}
//Make List
function makeList(item1, item2, item3) {
return list = [item1, item2, item3];
}
//Add to array
function addToList(list, item) {
var list = list;
list.push(item);
return list;
//Min-max
function max(numbers) {
var high = numbers[0];
for (var i = 0; i <= numbers.length; i++) {
if (numbers[i] > high) {
high = numbers[i];
}
}
return high;
}
What is scope? Your explanation should include the idea of global vs. local scope.
Scope is the namespace of a data element. Global scope encompasses the entire code project; global values can be called anywhere within the project. Local scope is contained with a particular function; other functions cannot access those values. It also means that functions within functions cannot be accessed outside the containing function.
Why are global variables avoided?
Global variables can overstep their bounds and cause unintended side effects if they are manipulated in a way that wasn't planned for. For example, if a developer has a global variable and a local variable with the same name, she may forget that fact and call the global variable instead of the local variable, changing the global value and ultimately changing the result of the program.
Explain JavaScript's strict mode
//Object creator
function createMyObject() {
return {
foo: "bar",
answerToUniverse: 42,
"olly olly": "oxen free",
sayHello: function() {
return "hello";
}
d1 = {"a":1, "b":2, "c":3}
d2 = {"x":4, "y":5, "z":6}
for key in d1:
d2[key] = d1[key]
# print(d2)
#{'x': 4, 'y': 5, 'z': 6, 'a': 1, 'b': 2, 'c': 3}
hardness_scale = {"talc": 1, "gypsum": 3, "calcite": 9}
minerals = {}
for a_key in hardness_scale.keys():
minerals[a_key] = hardness_scale[a_key]
print(minerals) #{'talc': 1, 'gypsum': 3, 'calcite': 9}
for a_key in hardness_scale:
minerals[a_key] = hardness_scale[a_key]
print(minerals) #{'talc': 1, 'gypsum': 3, 'calcite': 9}