This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 70%, bad performance | |
function solution(A) { | |
var leftSide | |
var rightSide | |
for (i = 0, arLen = A.length; i < arLen; i++) { | |
leftSide = 0; | |
rightSide = 0; | |
for (j = 0; j < i; j++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(A, K) { | |
if(A !== undefined && A.length > 0) | |
{ | |
for(let i =0; i < K; i++){ | |
A.unshift(A.pop()); | |
//unshift add element/s to begin of array and returns length of new array | |
//pop removes last element and returns that element (changes length of array) | |
} | |
} | |
return A; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(N) { | |
if (N < 1) { | |
return 0; | |
} | |
var start = 0; | |
var gap = 0; | |
var count = 0; | |
for (i = 0; i < 31; i++) { | |
if (N & (1 << i)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// first approach | |
function fibonacci(n) { | |
a = 0; | |
b = 1; | |
while (a <= n) { | |
console.log(a); | |
c = a + b; | |
a = b; | |
b = c; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NORMAL TRIANGLE left side | |
function triangle(n) { | |
for (i = 0; i < n; i++) { | |
for (j = 0; j < i + 1; j++) { | |
document.write(" * "); | |
} | |
document.write("</br>"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UPSIDE DOWN TRIANGLE | |
function asterisk(n) { | |
for (i = n - 1; i >= 0; i--) { | |
for (j = 0; j < n - i; j++) { | |
document.write("..."); | |
} | |
for (k = 0; k < i * 2 + 1; k++) { | |
document.write(" * "); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fac(n) { | |
if (n == 0) | |
return 1; | |
else | |
return fac(n - 1) * n; | |
} | |
fac(10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// looping variant | |
var power = function(base, exponent) { | |
var result = 1; | |
for (var count = 0; count < exponent; count++) | |
result *= base; | |
return result; | |
}; | |
console.log(power(2, 10)); | |
// recursive way (MUCH MUCH slower) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(myArr) { | |
var myArr = [9, 3, 9, 3, 9, 7, 9]; | |
var leng = myArr.length; | |
var result = []; | |
var leftObj = {}; | |
var i; | |
for (i = 0; i < leng; i++) { | |
leftObj[myArr[i]] = 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(myArr) { | |
var myArr = [9, 3, 9, 3, 9, 7, 9, 4, 5, 6, 8, 3, 4, 1, 2, 5, 6, 7, 7, 4, 2]; | |
var sortedArray = myArr.slice().sort(); | |
var pairedArray = []; | |
var leftUnpaired = []; | |
console.log(sortedArray); | |
var i; | |
for (i = 0; i < sortedArray.length - 1; i++) { | |
if (sortedArray[i + 1] == sortedArray[i] || sortedArray[i - 1] == sortedArray[i]) { |
OlderNewer