Created
September 19, 2018 11:02
-
-
Save yarkovaleksei/c3e41c1af662afeefaacea3f1b4c3987 to your computer and use it in GitHub Desktop.
jstest tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* See demo (https://jsfiddle.net/yarkov_aleksei/e4Lwoq1g/) | |
*/ | |
const braces = [['{', '}'], ['[', ']'], ['(', ')'], ['<', '>']] | |
const strBraces = braces.map(b => b.join('')).join('') | |
function isOpen(char) { | |
for (let i = 0; i < braces.length; i++) { | |
if (braces[i][0] === char) { | |
return true | |
} | |
} | |
return false | |
} | |
function matches(top, closed) { | |
for (let i = 0; i < braces.length; i++) { | |
if (braces[i][0] === top && braces[i][1] === closed) { | |
return true | |
} | |
} | |
return false | |
} | |
function checkBraces(str) { | |
if (!str) { | |
return 0 | |
} | |
const expression = str.split('') | |
let stack = [] | |
for (let i = 0; i < expression.length; i++) { | |
if (strBraces.includes(expression[i])) { | |
if (isOpen(expression[i])) { | |
stack.push(expression[i]) | |
} else { | |
if (stack.length === 0) { | |
return 1 | |
} | |
const top = stack.pop() | |
if (!matches(top, expression[i])) { | |
return 1 | |
} | |
} | |
} | |
} | |
return Number(!(stack.length === 0)) | |
} | |
function test() { | |
const data = [ | |
{ | |
Expression: '---(++++)----', | |
Actual: checkBraces('---(++++)----'), | |
Expected: 0 | |
}, | |
{ Expression: '', Actual: checkBraces(''), Expected: 0 }, | |
{ | |
Expression: 'before ( middle []) after ', | |
Actual: checkBraces('before ( middle []) after '), | |
Expected: 0 | |
}, | |
{ Expression: ') (', Actual: checkBraces(') ('), Expected: 1 }, | |
{ Expression: '} {', Actual: checkBraces('} {'), Expected: 1 }, | |
{ Expression: '<( >)', Actual: checkBraces('<( >)'), Expected: 1 }, | |
{ | |
Expression: '( [ <> () ] <> )', | |
Actual: checkBraces('( [ <> () ] <> )'), | |
Expected: 0 | |
}, | |
{ | |
Expression: ' ( [)', | |
Actual: checkBraces(' ( [)'), | |
Expected: 1 | |
} | |
] | |
console.table(data) | |
const testIsOk = data.filter(item => item.Expected === item.Actual) | |
console.log(testIsOk.length === data.length ? 'Test is OK' : 'Test failed') | |
} | |
test() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* See demo (https://jsfiddle.net/yarkov_aleksei/zvja46y3/) | |
*/ | |
const isObject = value => | |
typeof value !== 'function' && | |
value instanceof Object && | |
!Array.isArray(value) | |
function deepCopy(value) { | |
if (!isObject(value)) { | |
return value | |
} | |
const clone = {} | |
for (const i in value) { | |
if (isObject(value[i])) { | |
clone[i] = deepCopy(value[i]) | |
} else if (Array.isArray(value[i])) { | |
clone[i] = [...value[i]] | |
} else { | |
clone[i] = value[i] | |
} | |
} | |
return clone | |
} | |
function test() { | |
const testObject = { | |
item: 'Item name', | |
nestedObject: { | |
nestedKey: 'Key name' | |
}, | |
nestedArray: [1, 2, 3] | |
} | |
const cloneObj = deepCopy(testObject) | |
const testIsOk = | |
cloneObj.nestedObject !== testObject.nestedObject && | |
cloneObj.nestedArray !== testObject.nestedArray | |
console.log(testIsOk ? 'Test is OK' : 'Test failed') | |
} | |
test() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* See demo (https://jsfiddle.net/yarkov_aleksei/2daj3s4y/) | |
*/ | |
// Before | |
function func(s, a, b) { | |
if (s.match(/^$/)) { | |
return -1 | |
} | |
var i = s.length - 1 | |
var aIndex = -1 | |
var bIndex = -1 | |
while (aIndex == -1 && bIndex == -1 && i > 0) { | |
if (s.substring(i, i + 1) == a) { | |
aIndex = i | |
} | |
if (s.substring(i, i + 1) == b) { | |
bIndex = i | |
} | |
i = i - 1 | |
} | |
if (aIndex != -1) { | |
if (bIndex == -1) { | |
return aIndex | |
} else { | |
return Math.max(aIndex, bIndex) | |
} | |
} | |
if (bIndex != -1) { | |
return bIndex | |
} else { | |
return -1 | |
} | |
} | |
// After | |
function func(s, a, b) { | |
if (!s || (!a || !b)) { | |
return -1 | |
} | |
return Math.max(s.lastIndexOf(a), s.lastIndexOf(b)) | |
} | |
function test() { | |
const one = func('string', 'st', 'ng') | |
const two = func('', 'st', 'ng') | |
const three = func('string', '', 'ng') | |
const four = func('string', 'st', '') | |
const testIsOk = one === 4 && two === -1 && three === -1 && four === -1 | |
console.log(testIsOk ? 'Test is OK' : 'Test failed') | |
} | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment