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

24th Dec 2016, 9:11 PM
Nashwan Aziz
Nashwan Aziz - avatar
6 odpowiedzi
+ 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")));};
24th Dec 2016, 9:26 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 6
function countv2(t,v) { var r=new RegExp(v,"g"); return t.match(r).length; } But it's repetition :D
24th Dec 2016, 9:18 PM
visph
visph - avatar
+ 5
Not sure if he wants the ignore-case flag...
24th Dec 2016, 9:20 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
Your're right: I'll edit ^^
24th Dec 2016, 9:20 PM
visph
visph - avatar
+ 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 ;)
24th Dec 2016, 10:05 PM
visph
visph - avatar
+ 2
not out but
24th Dec 2016, 9:22 PM
Nashwan Aziz
Nashwan Aziz - avatar