Skip to content

Instantly share code, notes, and snippets.

@johndavedecano
johndavedecano / largest_consecutive_product.js
Created January 26, 2018 08:53
To Find Largest Consecutive Product Of Numbers In 1D Array
export function solution(given) {
var highest = [0, 0, 0];
for (var i=0; i<given.length; i++) {
var prev = given[i];
var next = !given[i + 1] ? 0 : given[i + 1];
var total = prev * next;
if (total > highest[2]) {
highest = [prev, next, prev * next];
}
}
@johndavedecano
johndavedecano / mention.jsx
Created January 11, 2018 14:57
mention.jsx
import React, { Component } from 'react';
import Popper from 'popper.js';
class RangeRef {
rect = {};
constructor(rect) {
if (rect) {
this.rect = rect;
}
@johndavedecano
johndavedecano / asterisk_pyramid.js
Created December 11, 2017 09:33
EXAMS - Asterisk Pyramid
function solution(height) {
var tree = [];
for (var i=0; i<5; i++) {
var child = "*";
for(var s=0; s<i; s++) {
child = child + "*"
}
tree[i] = child;
}
@johndavedecano
johndavedecano / odd_occurences.js
Created October 1, 2017 19:36
EXAMS - ODD OCCURENCES
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0)
var B = [];
for(var i = 0; i < A.length; i++){
var index = B.indexOf(A[i]);
if(index > -1) B.splice(index, 1);
else B.push(A[i]);
}
return B[0];
}
@johndavedecano
johndavedecano / distinct_values.js
Created October 1, 2017 18:09
EXAMS - DISTINCT VALUES
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 6.4.0)
return A.filter((x, y, z) => z.indexOf(x) === y).length;
}
@johndavedecano
johndavedecano / binary_gap_2.js
Last active October 1, 2017 18:03
EXAMS - Binary Gap
function solution(N) {
// write your code in JavaScript (Node.js 6.4.0)
var str = (N >>> 0).toString(2).split('');
var items = [];
var flag = false;
for (var i=0; i<str.length; i++) {
if (str[i] !== "0") {
flag = false;
@johndavedecano
johndavedecano / tape_equilibrium.js
Created October 1, 2017 08:00
EXAMS - Tape Equilibrium
function solution(A) {
var p, idx;
var leftSum = 0, rightSum = 0;
var totalSum = 0;
var lastMin, currentMin;
var N = A.length;
if (N == 2) { return Math.abs(A[0] - A[1]); }
if (N == 1) { return Math.abs(A[0]); }
@johndavedecano
johndavedecano / cyclic_rotation.js
Created October 1, 2017 07:56
EXAMS - Cyclic Rotation
function solution(A, K) {
// write your code in JavaScript (Node.js 6.4.0)
var TMP = A;
for (var i=0; i<K; i++) {
var PART = TMP.slice(1, -1);
var FIRST = TMP.slice(0, 1);
var LAST = TMP.slice(-1);
@johndavedecano
johndavedecano / missing_integer.js
Created October 1, 2017 07:25
EXAMS - Missing Integer
function solution(A) {
var min = 1;
A.sort(function(a,b){
return a - b;
});
for (var i in A) {
if (A[i] > -1 && A[i] === min) {
min++;
@johndavedecano
johndavedecano / frog_jumps.js
Created October 1, 2017 06:42
EXAMS - FROG JUMPS
function solution(x, y, d) {
const afterFirstJump = (85 - 10);
if (afterFirstJump % d == 0) {
return Math.floor(afterFirstJump / d);
}
return Math.floor(afterFirstJump / d + 1);
}