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() }) |
View counter.css
.counter { | |
margin: 15px; | |
} | |
.counterComponent__input { | |
border: 0; | |
font-size: 24px; | |
text-align: center; | |
font-weight: bold; | |
width: 60px; | |
} |
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); |
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; |
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]; |
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(' '); | |
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; |
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); | |
} | |
}); |
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; | |
} |
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 />', () => { |
NewerOlder