Skip to content

Instantly share code, notes, and snippets.

@bretdavidson
Last active June 12, 2017 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bretdavidson/6442699 to your computer and use it in GitHub Desktop.
Save bretdavidson/6442699 to your computer and use it in GitHub Desktop.
Possible Interview Questions
// Remove elements of second array from the first array
// Ex. 1 JavaScript Filter (no IE 8 support)
var arr1 = ['A', 'B', 'C', 'D', 'E', 'F'],
arr2 = ['C', 'E', 'F'],
filteredArray;
filteredArray = arr1.filter(function(e, i) {
return !(arr2.indexOf(e) > -1);
});
console.log('difference', filteredArray);
// Ex. 2 Basic for loops (modifies original array)
var arr3 = ['A', 'B', 'C', 'D', 'E', 'F'],
arr4 = ['C', 'E', 'F'];
for (var i = 0; i < arr4.length; i += 1) {
for (var j = 0; j < arr3.length; j += 1) {
if (arr4[i] === arr3[j]) {
arr3.splice(j, 1);
}
}
}
console.log('difference', arr3);
// Ex. 3 use Underscore/LoDash _.difference method
var arr5 = ['A', 'B', 'C', 'D', 'E', 'F'],
arr6 = ['C', 'E', 'F'],
difference;
difference = _.difference(arr5, arr6);
console.log('difference', difference);
for (var i = 1; i <= 100; i++) {
if (i % 15 == 0) {
console.log("FizzBuzz");
} else if (i % 3 == 0) {
console.log("Fizz");
} else if (i % 5 == 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
// Find the min/max value in an array
// Ex. 1 Math Object
var myArray,
max,
min;
myArray = [1, 2, 3, 4, 5];
max = Math.max.apply(null, myArray);
min = Math.min.apply(null, myArray);
console.log('max', max);
console.log('min', min);
// Ex. 2 Sort
var myArray1,
max1,
min1;
myArray1 = [2, 4, 5, 3, 1];
function sortNum(a, b) {
return a - b;
}
myArray1.sort(sortNum);
max1 = myArray1[myArray.length - 1];
min1 = myArray1[0];
console.log('max', max1);
console.log('min', min1);
// Ex. 3 Use Underscore/LoDash min/max methods
var myArray2,
max,
min;
myArray2 = [2, 4, 5, 3, 1];
console.log('max', _.max(myArray2));
console.log('min', _.min(myArray2));
// Swap the values of two variables
// Ex. 1 Temp variable
var a = 1,
b = 2,
tmp;
tmp = a;
a = b;
b = tmp;
console.log('a', a);
console.log('b', b);
// Ex. 2 XOR operator
var c = 1,
d = 2;
c = c ^ d;
d = c ^ d;
c = c ^ d;
console.log('c', c);
console.log('d', d);
// Ex. 3 ECMAScript 6
var e = 1,
f = 2;
[e, f] = [f, e];
console.log('e', e);
console.log('f', f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment