Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. "use strict";
  2. function dscount(string, a, b) {
  3. string = string.toUpperCase();
  4. a = a.toUpperCase();
  5. b = b.toUpperCase();
  6. var target = a + b;
  7. var count = 0;
  8. var pos = 0;
  9. while (true) {
  10. if (string.indexOf(target, pos) >= 0) {
  11. count += 1;
  12. pos = string.indexOf(target, pos) + 1;
  13. } else {
  14. return count;
  15. }
  16. }
  17. }
  18.  
  19.  
  20.  
  21.  
  22.  
  23. console.log(dscount('____cd____cd_______Cd____', 'c', 'd'));
  24. // alert('____CD____CD_______CD____'.indexOf('CD'));
  25. // Для удобства можно использовать эти тесты:
  26. try {
  27. test(dscount, ['ab___ab__', 'a', 'b'], 2);
  28. test(dscount, ['___cd____', 'c', 'd'], 1);
  29. test(dscount, ['de_______', 'd', 'e'], 1);
  30. test(dscount, ['12_12__12', '1', '2'], 3);
  31. test(dscount, ['_ba______', 'a', 'b'], 0);
  32. test(dscount, ['_a__b____', 'a', 'b'], 0);
  33. test(dscount, ['-ab-аb-ab', 'a', 'b'], 2);
  34. test(dscount, ['aAa', 'a', 'a'], 2);
  35.  
  36. console.info("Congratulations! All tests success passed.");
  37. } catch(e) {
  38. console.error(e);
  39. }
  40.  
  41. // Простая функция тестирования
  42. function test(call, args, count, n) {
  43. let r = (call.apply(n, args) === count);
  44. console.assert(r, `Finded items count: ${count}`);
  45. if (!r) throw "Test failed!";
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement