Skip to content

Instantly share code, notes, and snippets.

@Lelith
Lelith / greedyalgorithm.js
Created March 12, 2020 12:17
non overlapping segments
function solution(A, B) {
// write your code in JavaScript (Node.js 8.9.4)
if(A.length === 1 ){
return 1;
} else if(A.length ===0){
return 0;
}
let nonOverlapping = 1;
let prevEnd = B[0];
@Lelith
Lelith / longest password
Created March 11, 2020 17:37
longest password (60% solution, i tought needs to contain characters AND numbers)
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function isOdd(num){return num%2}
function solution(S) {
let solution = -1;
const passwords = S.split(' ');
@Lelith
Lelith / MissingNumber.js
Last active May 12, 2020 11:47
Find missing number runtime: O(N) or O(n*long(n))
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// set solution and index to 0
let solution = 0;
// sort array ascending to make finding the missing one easier
A.sort((a,b)=>a-b);
let index = 0;
while(solution === 0 && index <= A.length ){
solution = (A[index] !== (index+1) )? index+1 : 0;
@Lelith
Lelith / index.js
Created March 10, 2020 11:24
calculate object entries with recursion
function calcObjectLength(myObject, size) {
Object.entries(myObject).forEach(([, value]) => {
if (!isObject(value)) {
console.log('+1');
size += 1;
} else {
console.log('is object');
size += calcObjectLength(value, size);
}
});
@Lelith
Lelith / binaryGap.js
Created March 7, 2020 06:57
binary gap solution
function solution(N) {
// write your code in JavaScript (Node.js 8.9.4)
let binaryGaps = (N).toString(2); // returns binary string
binaryGaps = binaryGaps.replace(/^0*/,''); // remove leading zeros (no gap)
binaryGaps = binaryGaps.replace(/(0*$)/, ''); // remove trailing zeros (no gap)
binaryGaps = binaryGaps.split(1); // split on 1 to get gaps
const longestGap = Math.max(...(binaryGaps.map(gap => gap.length))); // get maximum number
return longestGap;
}
@Lelith
Lelith / snapshot.test.js
Last active April 18, 2020 08:12
Snapshot Tesz
/* src/components/Counter/test/Counter.test.js */
import React from 'react'
import Counter from '../';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json'
const callBackFunc = jest.fn();
const counterComponent = shallow(<Counter callBack = {callBackFunc} />);
describe('<counterComponent />', () => {
@Lelith
Lelith / counter.js
Created February 14, 2020 09:44
Call callback in componentDidUpdate
/* src/components/Counter/index.js */
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames/bind';
import './index.css';
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
@Lelith
Lelith / counter.test.js
Created February 14, 2020 09:43
Mock Function
describe('Counter', ()=> {
const callBackFunc = jest.fn();
const counterComponent = shallow(<Counter callBack = {callBackFunc} />);
(...)
it('calls a callback function with the current amount', () => {
expect(callBackFunc).toHaveBeenCalledTimes(2);
expect(callBackFunc).toHaveBeenLastCalledWith(0);
});
@Lelith
Lelith / counter.js
Created February 14, 2020 09:41
Decrease amount function
decreaseAmount() {
this.setState(prevState => (
{amount: prevState.amount -1})
)
}
@Lelith
Lelith / counterComponent.test.js
Created February 14, 2020 09:41
Decrease test
it('decreases the amount on minus interaction', () => {
const decreaseFunc = jest.spyOn(Counter.prototype, 'decreaseAmount');
counterComponent.find("[aria-label='decrease']").first().simulate('click');
expect(decreaseFunc).toHaveBeenCalledTimes(1);
expect(counterComponent.find("[aria-label='current amount']").first().props().value).toBe(0);
});