Skip to content

Instantly share code, notes, and snippets.

Avatar

Kathrin Holzmann Lelith

  • Berlin
View GitHub Profile
@Lelith
Lelith / setupTests.js
Created Apr 21, 2020
Jest Setup File
View setupTests.js
/* src/setupTests.js */
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';
import { configure } from "enzyme"
import Adapter from "enzyme-adapter-react-16"
configure({ adapter: new Adapter() })
@Lelith
Lelith / counter.css
Last active Apr 18, 2020
Counter CSS
View counter.css
.counter {
margin: 15px;
}
.counterComponent__input {
border: 0;
font-size: 24px;
text-align: center;
font-weight: bold;
width: 60px;
}
@Lelith
Lelith / counterComponentClassNames.js
Last active Apr 18, 2020
Counter component switching classes
View counterComponentClassNames.js
/* 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);
@Lelith
Lelith / tieRopes.js
Created Mar 12, 2020
tie ropes together
View tieRopes.js
function solution(K, A) {
let ropes = 0;
let length = 0;
A.forEach(rope => {
length += rope;
if(length >=K){
ropes +=1;
length = 0;
@Lelith
Lelith / greedyalgorithm.js
Created Mar 12, 2020
non overlapping segments
View greedyalgorithm.js
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 Mar 11, 2020
longest password (60% solution, i tought needs to contain characters AND numbers)
View longest password
// 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
Find missing number runtime: O(N) or O(n*long(n))
View MissingNumber.js
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 Mar 10, 2020
calculate object entries with recursion
View index.js
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 Mar 7, 2020
binary gap solution
View binaryGap.js
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 Apr 18, 2020
Snapshot Tesz
View snapshot.test.js
/* 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 />', () => {