+ 2
9
9. Write a JavaScript function that accepts two arguments, a string and a letter and the function will count the number of occurrences of the specified letter within the string. Sample arguments : 'w3resource.com', 'o' Expected output : 2 .
6 Answers
+ 8
put @visph 's function in js page and also add:
var t,v;
window.onload = function(){alert(countv2(prompt("insert string"),prompt("insert character")));};
+ 6
function countv2(t,v) {
var r=new RegExp(v,"g");
return t.match(r).length;
}
But it's repetition :D
+ 5
Not sure if he wants the ignore-case flag...
+ 3
Your're right: I'll edit ^^
+ 3
@ValentinHacker:
I see just now, but on one hand your "var t,v;" is unnecessary ( these are 2 globals variables, but in my function "t" and "v" are parameters variables ), and on the other hand it's in my code where it's more valid to add a var statement for the "r" variable ;)
+ 2
not out but