Skip to content

Instantly share code, notes, and snippets.

View misterpoloy's full-sized avatar
👽
Software Engineer

Juan P. Ortiz misterpoloy

👽
Software Engineer
View GitHub Profile
@misterpoloy
misterpoloy / ArrayOfProduct.js
Created September 3, 2021 07:46
Array of products excluding the current index
function arrayOfProducts(array) {
const resultArray = new Array(array.size)
const leftArray = new Array(array.size)
const rigthArray = new Array(array.size)
// Generate right array
let leftProduct = 1;
for (let i = 0; i < array.length; i++) {
leftArray[i] = leftProduct
leftProduct = leftProduct * array[i]
@misterpoloy
misterpoloy / tournamentWinner.js
Created September 3, 2021 07:06
Count match winner
function tournamentWinner(competitions, results) {
// Write your code here.
let maxWinnerCounter = Number.MIN_VALUE
let maxWinnerKey = ""
const resultsCounter = {};
console.log("bacon", results);
for (let i = 0; i < results.length; i++) {
const winnerKey = results[i] ? competitions[i][0] : competitions[i][1]
console.log("winnerKey", winnerKey);
// store counter
@misterpoloy
misterpoloy / SortedSquare.js
Created September 3, 2021 06:45
Sorted Square Array
function sortedSquaredArray(array) {
// Write your code here.
const squareResult = new Array(array.length)
let leftPointer = 0
let rightPointer = array.length - 1
// The important part is to reverse the array
for (let i = rightPointer; i >= 0; i--) {
let leftValue = Math.abs(Math.pow(array[leftPointer], 2))
let rightValue = Math.abs(Math.pow(array[rightPointer], 2))
squareResult[i] = Math.max(leftValue, rightValue)
@misterpoloy
misterpoloy / PolyfillBind.js
Created May 30, 2021 21:07
Polyfill for bind method
const person = {
name: "Juan",
lastName: "Ortiz",
}
const printName = function() { // Important this can't be arrow function
console.log(`${this.name} ${this.lastName}`);
}
const printPerson = printName.bind(person);
@misterpoloy
misterpoloy / maxSubArraySum.js
Created May 23, 2021 01:58
Slidding window pattern
function maxSubarraySum(arr, num) {
let maxSum = 0;
let tempSum = 0;
if (arr.length < num) { return null }
for (let i = 0; i < num; i++) {
maxSum += arr[i]
}
tempSum = maxSum;
for (let i = num; i < arr.length; i++) {
tempSum = tempSum - arrp[i - num] + arr[i];
@misterpoloy
misterpoloy / countUniqueValues.js
Created May 21, 2021 21:20
2 pointers pattern: Count Unique values
function countUniqueValues(numbers){
// add whatever parameters you deem necessary - good luck!
if (!numbers.length) return 0;
let left = 0;
let right = 1;
let counter = 1;
while (right < numbers.length) {
if (numbers[left] !== numbers[right]) {
@misterpoloy
misterpoloy / Sumzero.js
Created May 21, 2021 21:14
Two pointer patter sumzero
Function sumZero(arr) {
let left = 0;
let right = aro.length -1;
while (left < right) {
let sum = arr[left] + arr[right];
if (sum == 0) {
return [arr[left]], arr[right]
} else {
right—;
} else {
@misterpoloy
misterpoloy / Anagram.js
Created May 21, 2021 20:57
Function to detect 2 anagrams - Frequency counter method
function validAnagram(strA, strB){
if (strA.length !== strB.length) return false;
const strAcounter = {};
for (const char of strA) {
strAcounter[char] = (strAcounter[char] || 0) + 1;
}
const strBcounter = {};
for (const char of strB) {
strBcounter[char] = (strBcounter[char] || 0) + 1;
}
@misterpoloy
misterpoloy / DecoratorPattern.js
Created December 9, 2020 00:57
Decorator Pattern
class FrozenYoghurt {
constructor(flavor, price) {
this.flavor = flavor
this.price = price
}
orderPlaced() {
console.log(`The ${this.flavor} flavor will cost you ${this.price} dollars`);
}
}
@misterpoloy
misterpoloy / AbstractPattern.js
Created November 27, 2020 06:52
Abstract Pattern
function Soda(name,type,price) {
this.name = name;
this.type = type;
this.price = price;
this.display = function(){
console.log(`The ${this.type} ${this.name} costs ${this.price} dollars`)
}
}
function Chips(name,type,price) {