Skip to content

Instantly share code, notes, and snippets.

@jamesmontalvo3
Last active September 15, 2023 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmontalvo3/f8edc33dbda11f25a47d85769373a409 to your computer and use it in GitHub Desktop.
Save jamesmontalvo3/f8edc33dbda11f25a47d85769373a409 to your computer and use it in GitHub Desktop.
JS fundamental questions
/**
* The following examples help ensure understanding of some JavaScript fundamentals.
* Please make sure you understand these prior to the interview. Deep knowledge of JS is
* not required for the interview, but these fundamentals are.
*
* The third and final example is less about JS fundamentals, and is simply to show an
* example of using recursion. One interview question will involve recursion.
*
* You can try out running this code in your browser by opening the JavaScript console.
* Open developer tools and go to console. On some browsers/OS you can do ctrl+shift+j.
*/
/**
* Example 1
* ---------
* For understanding how variables are passed to functions, make sure you understand
* why one variable did not change and the others did.
*/
var myValue = 1;
var myRef = { a: 'Three Body Problem' };
function test(param1, param2) {
console.log('param1 = ' + param1);
console.log('myValue = ' + myValue);
console.log('param2 = ', param2);
console.log('myRef = ', myRef);
param1 = 'something new';
param2.a = 'A Memory Called Empire';
console.log('param1 = ' + param1); // changed
console.log('myValue = ' + myValue); // did not change, primitives (strings, numbers) are passed by value
console.log('param2 = ', param2); // changed
console.log('myRef = ', myRef); // changed, objects are passed by reference
return 'done';
}
console.log(test(myValue, myRef));
/**
* Example 2
* ---------
* For understanding how arrays and objects work, this code shows how to access
* and set values in arrays and objects.
*/
var myArray = [
'zeroth',
'first',
'second',
{ a: 'pineapple' }
];
var myObj = {
prop1: 'apple',
prop2: 'banana',
prop3: { subProp1: myArray }
};
console.log(myArray[0]); // prints 'zeroth'
console.log(myArray[3].a); // prints 'pineapple'
console.log(myObj.prop1); // prints 'apple'
console.log(myObj['prop1']); // also prints 'apple'
console.log(myObj.prop3.subProp1[3].a); // prints 'pineapple'
myObj.prop3.subProp1[3].somethingNew = 'mango';
console.log(myArray[3].somethingNew); // prints 'mango'
/*
* Example 3
* ---------
* This shows how recursion works. This isn't really a JS-specific thing, but it is important
* that you understand it for the interview.
*/
var myDeepObject = {
prop: {
prop: {
prop: {
prop: {
prop: {
prop: 'aardvark'
}
}
}
}
}
};
var myShallowObject = {
prop: 'binturong'
};
function findAnimal(x) {
if (typeof x === 'string') {
return x;
}
return findAnimal(x.prop);
}
console.log(findAnimal(myDeepObject)); // prints 'aardvark'
console.log(findAnimal(myShallowObject)); // prints 'binturong'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment