here are the questions i was asked.. Please help Im a little confussed.

6.Describe (3-4 sentences) what this statement does: ("this is foo bar".match(/o/g)||[]).length

7.Write a JavaScript function that takes in a DOM element and returns a count of the number of <a> tags contained within that element (first level count only, non-recursive).

Am I on the right track with this code

#1.
("this is foo bar".match(/o/g)||[]).length
//>2
#2.
"this is foo bar".split("o").length-1
//>2
//split is not recommended. Resource hungry. Allocates new instances of 'Array' for each match. Don't try that for a >100MB file via FileReader.
//You can actually easily observe the EXACT resource usage using **Chrome's profiler** option.


#3.
var stringsearch = "o"
,str = "this is foo bar";
for(var count=-1,index=0; index != -1; count++,index=str.indexOf(stringsearch,index+1) );
//>count:2
#4.
//searching for a single character

var stringsearch = "o"
,str = "this is foo bar";
for(var i=count=0; i<str.length; count+=+(stringsearch===str[i++]));
//>count:2
  1. this statement will match the letter 'o' in the previous statement and return the length of the resulting array which is 2.

  2. function tagcount(element)
    {
    var searcharea = document.getElementById(element);
    var count = searcharea.getElementByTagName('a').length;
    }

you are awesome.. number one I got .. And good friend tried to explain number two to me but he confused me even more. lol I really over thought this.Thank you so much..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.