Program:
//method definition
function countString(str, chr){
var count = 0;
for(var k=0; k < str.length; k++){
if(str.charAt(k) === chr){
count++;
}
}
return count;
}
//method call
countString("Hello World", "l");
Output:
3
Program:
//method definition
function countString(str, chr){
var count = 0;
for(var k=0; k < str.length; k++){
if(str.charAt(k) === chr){
count++;
}
}
return count;
}
//method call
countString("Hello World", "l");
Output:
3
Qus 1. Given a string "Hello" & write a code to print the count of the chars available to it & create the object like this
{"H":1, "e":1, "l":2, "o":1}
Soln:
var word = "Hello";
temp = [];
arr.forEach(function(chr){
temp[chr] = word.split(chr).length - 1;
});
var finalObj = Object.assign({}, temp);
console.log(finalObj);
Qus 2. Output of the below given program -
Program:
console.log(1);
console.log(2);
setTimeout(()=>{
console.log(3);
}, 3000);
console.log(4);
console.log(5);
Output:
1
2
4
5
3
Qus 3. Output of the below given program & what if replaces the inner function as a arrow method -
Program:
var personObj = {
first_name: 'Alex',
last_name: 'Waugh',
getName: function(){
console.log( this.first_name, this.last_name)
}
}
personObj.getName();
Output:
Alex Waugh
//if replaced the getName to arrow method then what will be the output
undefined undefined