Last active
March 30, 2017 15:28
-
-
Save karataev/60e9f7df08f45b249f084a2279289a38 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
"use strict"; | |
console.clear(); | |
// Yor code here ... | |
function dscount(str, s1, s2) { | |
const pattern = `${s1}${s2}`; | |
const regexp = new RegExp(pattern, 'gi'); | |
let matches; | |
let counter = 0; | |
while((matches = regexp.exec(str)) != null) { | |
regexp.lastIndex = regexp.lastIndex - pattern.length + 1; | |
counter++; | |
} | |
return counter; | |
} | |
// Для удобства можно использовать эти тесты: | |
try { | |
test(dscount, ['ab___ab__', 'a', 'b'], 2); | |
test(dscount, ['___cd____', 'c', 'd'], 1); | |
test(dscount, ['de_______', 'd', 'e'], 1); | |
test(dscount, ['12_12__12', '1', '2'], 3); | |
test(dscount, ['_ba______', 'a', 'b'], 0); | |
test(dscount, ['_a__b____', 'a', 'b'], 0); | |
test(dscount, ['-ab-аb-ab', 'a', 'b'], 2); | |
test(dscount, ['aAa', 'a', 'a'], 2); | |
console.info("Congratulations! All tests success passed."); | |
} catch(e) { | |
console.error(e); | |
} | |
// Простая функция тестирования | |
function test(call, args, count, n) { | |
let r = (call.apply(n, args) === count); | |
console.assert(r, `Finded items count: ${count}`); | |
if (!r) throw "Test failed!"; | |
} |
This file contains hidden or 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
function parseUrl(url) { | |
var linkEl = document.createElement('a'); | |
linkEl.href = url; | |
return linkEl; | |
} | |
let a = parseUrl('http://tutu.ru:8080/do/any.php?a=1&b[]=a&b[]=b#foo') | |
// Вернет объект, в котором будут следующие свойства: | |
console.log( a.href == "http://tutu.ru:8080/do/any.php?a=1&b[]=a&b[]=b#foo" ) | |
console.log( a.hash == "#foo" ) | |
console.log( a.port == "8080" ) | |
console.log( a.host == "tutu.ru:8080" ) | |
console.log( a.protocol == "http:" ) | |
console.log( a.hostname == "tutu.ru" ) | |
console.log( a.pathname == "/do/any.php" ) | |
console.log( a.origin == "http://tutu.ru:8080" ) |
This file contains hidden or 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
'use strict'; | |
console.clear(); | |
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; | |
} | |
} | |
function funcRefactored(s, a, b) { | |
const aIndex = a.length === 1 ? s.lastIndexOf(a) : -1; | |
const bIndex = b.length === 1 ? s.lastIndexOf(b) : -1; | |
const maxIndex = Math.max(aIndex, bIndex); | |
return maxIndex > 0 ? maxIndex: -1; | |
} | |
var args = [ | |
['hello', 'e', 'e'], | |
['hello', 'H', 'll'], | |
['hello', 'z', 'x'], | |
['hello', 'o', 'l'], | |
]; | |
args.forEach(item => { | |
console.log(func.apply(null, item), funcRefactored.apply(null, item)); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment