Skip to content

Instantly share code, notes, and snippets.

@mtthwgry
Last active August 29, 2015 14:16
Show Gist options
  • Save mtthwgry/3f8a5b363f9c64ba848f to your computer and use it in GitHub Desktop.
Save mtthwgry/3f8a5b363f9c64ba848f to your computer and use it in GitHub Desktop.
Prep for WealthFront
// JavaScript variables belong to one of the following scopes: global or local(function).
// Basically, any variable defined outside of a function is a global variable. Variables
// defined inside of a function are scoped narrowly to that function, and any other
// functions defined within the scope of that function (explicit use of the var keyword assumed).
var myName = "john";
var capitalizeMyName = function() {
myName = myName.substring(0).toUpperCase() + myName.slice(1);
var name = myName;
}
capitalizeMyName();
console.log(myName);
console.log(name);
// What will happen to the myName variable? Why? What is expected value should console.log(name) return?
// The myName variable will be assigned the value "John" when capitalizeMyName() is called,
// because inside the function we are referencing the global variable myName. The value console.log(name)
// should return undefined, because in name variable doesn't exist in the global scope.
// This answer shows that the candidate understands how JavaScript handles scope.
var palindromeChecker = function(word) {
for(var i=0, j=word.length-1; i < j; i++, j--) {
if(word.charAt(i) !== word.charAt(j)) {
return false;
}
}
return true;
}
var palindromeCheckerRecursive = function(word) {
if(word.length <= 1) {
return true;
} else {
if(word.charAt(0) === word.charAt(word.length - 1)) {
return palindromeCheckerRecursive(word.substring(1, word.length - 1));
} else {
return false;
}
}
}
var Person = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.fullName = function() {
return this.firstName + " " + this.lastName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment