Skip to content

Instantly share code, notes, and snippets.

@vvscode
Forked from pstachula-dev/gist:91afafb4df4e0dff5f40
Last active August 29, 2015 14:27
Show Gist options
  • Save vvscode/78944c1251f1d3088e57 to your computer and use it in GitHub Desktop.
Save vvscode/78944c1251f1d3088e57 to your computer and use it in GitHub Desktop.
Interview
// Source http://www.thatjsdude.com/interview/js1.html
/*
|--------------------------------------------------------------------------
| Log p()
|--------------------------------------------------------------------------
|
*/
var p = function(a) {
console.log(a);
};
/*
|--------------------------------------------------------------------------
| Spacify
|--------------------------------------------------------------------------
|
*/
function spacify(args) {
return args.split("").join(" ");
}
String.prototype.spacify = function(args) {
return this.split("").join(" ");
};
// p("Hello".spacify());
/*
|--------------------------------------------------------------------------
| Check Prime
|--------------------------------------------------------------------------
|
*/
function isPrime(args) {
var d = 2;
while(args > d) {
if(args % d++ === 0) return false;
}
return true;
}
// p(isPrime(7));
/*
|--------------------------------------------------------------------------
| Fibonacci
|--------------------------------------------------------------------------
|
*/
function fibonacci(args) {
var score = 0,
val = 1;
while(args--) {
score += val;
val = score - val;
}
return score;
}
// p(fibonacci(6));
/*
|--------------------------------------------------------------------------
| Greatest Common Divisor
|--------------------------------------------------------------------------
|
*/
function greatestDivisor(arg1, arg2) {
var divisor = 2,
score;
while(arg1 >= divisor && arg2 >= divisor) {
if(arg1 % divisor === 0 && arg2 % divisor === 0) {
score = divisor;
}
divisor++;
}
return score;
}
// p(divisor(14,21));
/*
|--------------------------------------------------------------------------
| Duplicate
|--------------------------------------------------------------------------
|
*/
// Array.prototype.contains = function(args) {
// var len = this.length;
// while(len--) if(this[len] == args) return true;
// return false;
// }
function duplicate(args) {
var score=[],
temps=[],
len = args.length;
for(i=0; i<len; i++) {
val = args[i];
if(temps[val] !== false) {
score.push(val);
temps[val] = false;
}
}
return score;
}
// p(duplicate([1,1,2,3,4,4,4,5,1]));
/*
|--------------------------------------------------------------------------
| Merge array
|--------------------------------------------------------------------------
|
*/
function mergeArr(arg1, arg2) {
return arg1.concat(arg2).sort(function(a,b){
return a - b;
});
}
// p(mergeArr([2,5,29], [1,3,7,12]));
/*
|--------------------------------------------------------------------------
| String Reverse
|--------------------------------------------------------------------------
|
*/
function reverse(arg) {
if(arg && typeof arg === 'string') {
return arg.split("").reverse().join("");
}
}
// p(reverse("Siemka"));
/*
|--------------------------------------------------------------------------
| Reverese in place
|--------------------------------------------------------------------------
|
*/
function reverseInPlace(args){
var score = [];
args = args.split(" ");
for(i=0; i<args.length; i++) {
score.push(args[i].split("").reverse().join(""));
}
return score.join(" ");
}
// p(reverseInPlace('I am the good boy'));
/*
|--------------------------------------------------------------------------
| First non repeating char
|--------------------------------------------------------------------------
|
*/
function firstNonRepeatingChar(arg) {
var val,
len = arg.length,
arr = [];
for(i=0; i<len; i++) {
val = arg[i];
if(arr[val]) {
arr[val]++;
} else {
arr[val] = 1;
}
}
for(var j in arr) {
if(arr[j] == 1) return j;
}
}
// p(firstNonRepeatingChar("The Javascript"));
/*
|--------------------------------------------------------------------------
| Remove duplicate char
|--------------------------------------------------------------------------
|
*/
function removeDuplicateChar(arg) {
var val,
len = arg.length,
arr = [],
score = [];
for(i=0; i<len; i++) {
val = arg[i];
if(arr[val]) {
arr[val]++;
} else {
arr[val] = 1;
}
}
for(var j in arr) {
if(arr[j] == 1) {
score.push(j);
}
}
return score.join("");
}
// p(removeDuplicateChar("Learn more javascript dude"));
/*
|--------------------------------------------------------------------------
| Check palindrome
|--------------------------------------------------------------------------
|
*/
function isPalindrome(arg) {
s1 = arg.split("").reverse().join("");
return s1 === arg;
}
// p(isPalindrome("madam"));
/*
|--------------------------------------------------------------------------
| Missing number
|--------------------------------------------------------------------------
|
*/
function missingNumber(arg) {
var val=0,
max=0,
len = arg.length;
for(i=0; i<len+2; i++) {
max += i;
if(i < len)
val += arg[i];
}
return max - val;
}
// p(missingNumber([5,2,6,1,3]));
/*
|--------------------------------------------------------------------------
| Largest Sum
|--------------------------------------------------------------------------
|
*/
function largestSum(arg) {
var len = arg.length;
arg = arg.sort(function(a,b){ return a-b; });
return arg[len-1] + arg[len-2];
}
// p(largestSum([6,4,3,2,1,7]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment