Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- function dscount(string, a, b) {
- string = string.toUpperCase();
- a = a.toUpperCase();
- b = b.toUpperCase();
- var target = a + b;
- var count = 0;
- var pos = 0;
- while (true) {
- if (string.indexOf(target, pos) >= 0) {
- count += 1;
- pos = string.indexOf(target, pos) + 1;
- } else {
- return count;
- }
- }
- }
- console.log(dscount('____cd____cd_______Cd____', 'c', 'd'));
- // alert('____CD____CD_______CD____'.indexOf('CD'));
- // Для удобства можно использовать эти тесты:
- 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!";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement