Javascript Interview Questions(Part-4)

JavaScript Coding interview questions with solutions:-

Question1:-

You would like to set a password for a bank account. However, there are three restrictions on the format of the password:

it has to contain only alphanumerical characters (az, AZ, 09);
there should be an even number of letters;
there should be an odd number of digits.
You are given a string S consisting of N characters. String S can be divided into words by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

For example, given "test 5 a0A pass007 ?xy1", there are five words and three of them are valid passwords: "5", "a0A" and "pass007". Thus the longest password is "pass007" and its length is 7. Note that neither "test" nor "?xy1" is a valid password, because "?" is not an alphanumerical character and "test" contains an even number of digits (zero).

Write a function:

int solution(char *S);
that, given a non-empty string S consisting of N characters, returns the length of the longest word from the string that is a valid password. If there is no such word, your function should return 1.

For example, given S = "test 5 a0A pass007 ?xy1", your function should return 7, as explained above.

Assume that:

N is an integer within the range [1..200];
string S consists only of printable ASCII characters and spaces.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.


Solution1:-

function solution(S) {
// write your code in JavaScript (Node.js 6.4.0)
var pwds = S.split(" ");
var pwdLen = -1;
pwds.forEach(function(pwd){
if(pwd.match(/^[a-zA-Z0-9]*$/)){
if(pwd.length%2!=1)
return;
var chrCnt = pwd.match(/[a-zA-Z]/g || []).length;
if(chrCnt%2!=0)
return;
if (pwd.length > pwdLen)
pwdLen = pwd.length;
}
});
return pwdLen;
}

Question2:-
Given four integers, display the maximum time possible in 24 hour format HH:MM. For example, if you are give A = 1, B = 9, C = 9, D = 2 then output should be 19:29. Max time can be 23:59 and min time can be 00:00.

If it is not possible to construct 24 hour time then return error. For example, given A = 1, B = 9, C = 7, D = 9 an error should be returned since minimum time represented by these integers is 17:99 which is “NOT POSSIBLE”?

Solution2:-

function solution(A, B, C, D) {
var arr = [];
var digit = [];
var timeString = "";
//create array from given numbers
arr.push(A);
arr.push(B);
arr.push(C);
arr.push(D);
digit[0] = findMax(arr,2);
digit[1] = digit[0]==2?findMax(arr,3):findMax(arr,9);
digit[2] = findMax(arr,5);
digit[3] = findMax(arr, 9);
//final number
if(digit[0] ==-1 ||digit[1] ==-1||digit[2] ==-1||digit[3] ==-1){
return "NOT POSSIBLE";
}
timeString = digit[0]+""+digit[1]+":"+digit[2]+""+digit[3];

return timeString;

}
//finding the less than equal number and return it
function findMax(arr, find){
if(arr.length!=4)
{
return -1;
}
var numToFind = -1;
var indexToRemove = -1;
//iterate arrary
for(var i = 0; i < arr.length;i++){
if(arr[i] <= find)
{
if(arr[i]> numToFind)
{
numToFind = arr[i];
indexToRemove =i;
}
}
}
if (indexToRemove == -1)
return -1;
arr[indexToRemove] = -1;
return numToFind;
}

Question3:-Given DOM tree I need to find the maximum depth of the nested ul/ol tags.
Example:-

<ul>
<li>Item:
<ol>
<li>Point:
<div>
<ul>
<li>elem1</li>
</ul>
</div>
</li>
</ol>
</li>
<li>elem2</li>
</ul>
<ul>
<li>simple list1</li>
</ul>
<ul>
</ul>
The depth would be 3

Solution3:-

function solution() {

var len, max_depth=0;
$('li:not(:has(ol)):not(:has(ul))').each(function(){
len = $(this).parents('ul,ol').length;
if(len > max_depth)
max_depth =len;
});
//console.log(max_depth);
return max_depth;
}

More Interview Questions

Javascript Interview Questions(Part 3)

Here comes more JavaScript questions with solutions.

Learn more share more, do follow us on FB, Twitter , G+ or LinkedIn.

Javascript interview Questions
What does NaN function do?
The isNaN() function determines whether a value is NaN or not. Coercion inside the isNaN function has interesting rules.When the argument to the function is not isNaN() of type Number.The value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN.

Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and Boolean primitives, which when coerced give numeric values zero or one), the “false” returned value may be unexpected; the empty string, for example, is surely “not a number.” 

Examples:


isNaN(NaN);       // true
isNaN(undefined); // true
isNaN({}); // true

isNaN(true);      // false
isNaN(null); // false
isNaN(37); // false

// strings
isNaN("37"); // false: "37" is converted to the number 37 which is not NaN
isNaN("37.37"); // false: "37.37" is converted to the number 37.37 which is not NaN
isNaN(""); // false: the empty string is converted to 0 which is not NaN
isNaN(" "); // false: a string with spaces is converted to 0 which 
                         is not NaN

// dates
isNaN(new Date()); // false
isNaN(new Date().toString()); // true

isNaN("blabla") // true: "blabla" is converted to a number.
// Parsing this as a number fails and returns NaN



falsy: In javascript 6 things are falsy and they are- false, null, undefined, ”, 0, NaN

NaN in JavaScript
falsy type


truthy: There are only two truthy things- true and everything that is not false

JavaScript true type in NaN Function
true type


What will be the output of the following JS codes?

1.
var b = 0.6
var c= 0.2
(a-b) == c ??

false because float result will always be different

2. ‘ ‘ == 0 ??

It will convert string to number and then perform comparison . If we blank string to number, it will return zero.
So it becomes 0 == 0 ,which will return true

3. [ ] == 0; ??

It will convert []to number and then perform comparison . If we blank [] to number, it will return zero.
You can check this by Number([]) will return  0.
So it becomes 0 == 0 ,which will return true

4. [ ] == [ ] ??

false because both will have different memory pointers.

5. null == null / null=== null??

true

6. null == undefined ??

true

7. null === undefined??

false. Because both have different types.
typeof(null) > object (It is a bug and cannot be fixed because it will break
typeof(undefined) > undefined

8. null instanceOf object ?? 
it will throw an error

9. Is ‘false’  return false?

No ,Because it is a string with length greater than 0.Empty string return false

10. 
Is ‘ ‘ return false?

No ,Because it is a string with an empty space.

11. What is the output of void(2+3)?
It will throw an error.

12. [0] == true

False

//How it works
//convert boolean using toNumber
[0] == 1
//convert object using toPrimitive
//[0].valueOf() is not a primitive so use
//[0].toString() –> “0”
“0” == 1
//convert string using toNumber
0 ==1 –> False


13. “ufthelp” == true

False

//How it works
//convert boolean using toNumber
“ufthelp” == 1
//convert string using toNUmber
NaN == 1 –> False


 

Difference between null and  undefined? 
null means empty or non-existent assignment value which is used by programmers to indicate “no value”. null is a primitive value and you can assign null to any variable.
Ex:
var test_var = null;
alert(test_var); //show null
alert(type of test_var); //shows object
Note: null is not an object,it is a primitive value.Sometimes people assume it wrongly that you can add properties to it because it typeof(null) returns “object”.

undefined means ,undefined means a variable has been declared but has not yet been assigned a value.
Ex:
var test_var;
alert(test_var); //show undefined
alert(type of test_var); //shows undefined


Remember,undefined is a type with exactly one value and that is undefined.You cannot change the value of it by assigning a new value.

What are the scenarios to get undefind?

  • A declared variable without assigning any value to it.Ex: var a;
  • Implicit returns of functions due to missing return statements
  • Return statements that explicitly do not return anything
  • Function parameters that are not passed 
  • Lookups for non-existent properties that do not exist
  • Anything that has been set to undefined
  • Any expression in the form of void

  Deep dive into null

JavaScript Questions-(Part-2)

Motivation to learn more and fly free
Prepare well to crack Interview’s



1.What is the functionality of DOM?

2.Tell different ways to refresh the page?
location.reload()
history.go()
window.location.href

3.How to assign value to ViewState?
ViewState.Add(“Name”,”UFT”);
ViewState[“Name”] = “UFT”;

4.What is the output of JS Code?
var title=”User Friendly Techy Help”;
var char = title[13];
alert(char);

5.How to make this string capitalize “UFT”;

6.What will be the output of JS Code?
var str = new String(“User Friendly Techy Help”);
alert(str.charAt(3));

var a=[‘apple’,’orange’,’banana’];
var b=[‘pineapple’, ‘guava’, ‘grapes’];
var c = a.concat(b);
alert(c[2]);

7.How to assign value to a textbox using JS?
document.getElementById(“textName”).value= “UFT”;

8.What is the output of “2” + 3 and “2” – 3 ?

9.Is “JavaScript”  a case-sensitive language?
Yes

10.What does the “noscript” tag do?


JavaScript Questions(Part-1)
More interview Questions