0

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).

2
Contributors
3
Replies
26
Views
3 Years
Discussion Span
Last Post by WoW Me Web Girl
0

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
0
  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;
    }

0

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..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.