1

I need some logic from your side.I want to count the number of element present n-between that starting and ending value Like

Input :Starting point in 1
          end point is :10

Search :2

count :1(only 2)

Input :Starting point in 1
          end point is :20

Search :2

count :3(only 2, 12 ,20)
function(var start,var end,var searchItem){
for(var start;start<=end;start++)

}

1 Answers1

0

Try,

function search(start, end, searchItem) {

    var xCnt = 0;

    for (var start = 0; start <= end; start++) {

        if ((start + '').indexOf(searchItem + '') >= 0) {
            xCnt += 1;
        }

    }
    return xCnt;
}

DEMO

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • correct but can you explain this logic?if((start+'').indexOf(searchItem+'') >=0) –  Dec 20 '13 at 17:25
  • @user944513 For example x.indexOf(y) will return the index of y in any substrings of x. If it do not find anything, it will return -1. For More reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf – Rajaprabhu Aravindasamy Dec 20 '13 at 17:28
  • @user944513 Ok so now explain why is this question is tagged with jQuery and Java when obviously is none of them? – A. Wolff Dec 20 '13 at 17:30
  • @user944513 you should have tagged this with javascript obviously. And i don't think you want extra comma – A. Wolff Dec 20 '13 at 17:35
  • 1
    @user944513 logic will be the same, search for index of character in string. But wait, aren't you confusing javascript with java? – A. Wolff Dec 20 '13 at 17:36