In this article we are going to discuss 2 javascript interview question and their answers.These questions are highly asked in the Javascript interviews.
Disclaimer: - You might have different solution working with all test cases for both the problems.
So lets discuss the first question.This question is to test your ability in terms javascript object knowledge and your logical thinking.
Q1. You have been given a nested object "Person" which has one of the property as "password".Write a generic function to change the value of all the "password" property to "*****". Object is nested and may contain other object with property "password" and so on. Here is a sample object.
var Person = {
name: 'Alex',
age : 40,
password : 'abc',
x : {
name : 'Chris',
password: 'abcd',
y : {
name : 'Mike',
password: 'abcd',
},
}
.
.
}
Before we jump into the solution, I want you to try yourself. First try with single object and then with one more level of object and then try writing a generic function.
So here is the solution
1. function nestedObjectIteration( obj ) {
2. for(var key in obj) {
3. if( obj.hasOwnProperty('password') ) {
4. obj['password'] = '*****';
5. }
6. if( typeof(obj[key]) == 'object' ) {
7. nestedObjectIteration(obj[key])
8. }
9. }
10. return obj;
11. }
So here I have written solution with recursion function(function which calls itself). We have looped through all the keys of the object and in line number 3 we are checking,if the property is 'password' we will change the value to '*****'.
In line 6, we are checking if its an object then we will call the same function, passing the object value.Finally we will return the obj.
Let now try to solve another problem.
Q. Write a program to count the occurrence of each character in a sentence.
Note: small letter and capital letter are treated different.i.e. 'P' and 'p' are treated as two different character.
So let say we have been given a sentence "himanshu is coding". Now we need to write a solution which can tell the occurrence of 'h', 'i','m'... so on.
All right so what should be the approach. First we need to have a datatype which can hold each character and there occurrence. In Java,people use hash map as a solution. Similar to hash map we have object in Javscript.
In the object we can store each character as a key and its occurrence as value.That means, we can actually return something like this {h: 2, i: 2, m: 1, a: 1, n: 1, …} as solution for the given sentence.So pause for a while and try writing the program.
Here we have the solution
1. var countStr = ( str ) => {
2. let arr = str.split('');
3. let obj ={};
4. for(let i=0; i < arr.length ; i++) {
5. if(obj[arr[i]]) {
6. obj[arr[i]]++;
7. } else {
8. obj[arr[i]] = 1;
9. }
10. }
11. return obj;
12. }
As explained, We have taken each character in an array( here arr) and we are looping through each characters and finding if that character exist in the object(here obj which is initially empty).
If the character exist we will increment the value else we will add the character in the object.
Hope this article was helpful. Subscribe and leave your comment to support.
Thank you!!
This is very helpful..
ReplyDelete