Skip to content

Instantly share code, notes, and snippets.

@encap
encap / keyboardLogic.js
Last active August 13, 2021 13:14
The Logic behind virtual keyboard component on CodeRush
computed: {
mistakesHistory() {
return this.history.filter((change) => change.type === 'mistake');
},
mistakes() {
return this.mistakesHistory.reduce((acc, mistake) => {
if (mistake.shift && mistake.expectedText !== ' ') {
if (mistake.text.toLowerCase() === mistake.expectedText
|| (mistake.text.match(/[~!@#$%^&*()_+{}:"|<>?]/)
&& mistake.expectedText.match(/[`1234567890-=[\];'\\,./]/)
@encap
encap / server.js
Created February 2, 2021 15:06
FaunaDB query to handle run stats (from server.js in CodeRush repo)
try {
const qResponse = await client.query(
[
q.Select(['data', 'total'],
q.Let(
{
lang: q.Get(q.Match(q.Index('languageByIndex'), main.languageIndex)),
ref: q.Select(['ref'], q.Var('lang')),
},
q.Update(q.Var('ref'),
@encap
encap / algorithm.js
Created February 2, 2021 14:54
Algorithm that receives two sequences: A and B of integers and returns one sequence C. Sequence C should contain all elements from sequence A (maintaining the order) except those, that are present in sequence B p times, where p is a prime number.
// inputs
const A = [2,3,9,2,5,1,3,7,10]
const B = [2,1,3,4,3,10,6,6,1,7,10,10,10]
const P = 2;
// expected results for that inputs
// [2,9,2,5,7,10]
// test larger input arrays
// for (let i = 0; i < 100000; i += 1) {
// A.push(Math.floor(Math.random()*1000))
@encap
encap / getNetworkAddress.js
Last active February 2, 2021 14:38
Calculate network address from ip and mask (fragment of network_ip_tool in cidr repo)
const [host, mask] = ev.target.value.split('/');
if (host.split('.').length !== 4 || mask === undefined || !mask.length ||isNaN(mask)) {
console.warn('invalid input')
return;
}
const arrBinHost = host.split('.').map((octet) => {
const octetArr = Number(octet).toString(2).split('');
const octetArrLength = octetArr.length