JavaScript Interview Question…


Write a program to Check number is Palindrome.

function checkPalindrome(str) {

    if(str) {
        var pattern = /[^a-zA-z]+/g; // first remove spaces and special chars from given string
        str = str.replace(pattern, '').toLowerCase();
        var str1 = str.split('').reverse().join('');
        console.log(str, str1); // for testing
        if(str1 == str.toLowerCase()) {
            return true;
        }else {
            return false;
        }
    }
}

Output >
checkPalindrone(“diD”)  
=> true


> Second way without using any predefined method:

 function checkPalin(str)  {
    
    var pattern = /[^a-zA-z]+/g;
    var output = "";
    var input = str.replace(pattern, '').toLowerCase();
    for(var item of input) {
        output = item  + output; 
    }
    if(input == output) {
        return true;
    }else {
        return false;
    }
} 

Output >    
 checkPalin(“Cigar? Toss it in a can. It is so tragic”)  
 result is true


Write a program to count duplicate characters in a string

 function duplicateCharCount(str) {
        
    if(str) {
        var obj = {};
        for(let i = 0; i < str.length; i++) {
            if(obj[str[i]]){
                obj[str[i]] += obj[str[i]];
            }else {
                obj[str[i]] = 1;
            }
        }
        console.log(obj);
    }
            
} 

Run this Code >   
duplicateCharCount(“aabcdd”);
Output >    {a: 2, b: 1, c: 1, d: 2}


Write a program to check two objects are same or not

 function equal(obj1, obj2) {

    let keys =  Object.keys(obj1); // first find keys of first Object
    let output = false;
    for (let i = 0 ; i < keys.length ; i++) {
            output = (obj2.hasOwnProperty(keys[i]) && obj1[keys[i]] == obj2[keys[i]]) ? true : false;
    }
    return output;

} 


var o1 = {b : 1, c : 2};
var o2 = {b: 1, c : 2};
equal(o1, o2);  –> true

var o1 = {b : 1, c : 2};
var o2 = {b: 1, c : 2 , d: 4};
equal(o1, o2);  –> false

Read here for Angular Interview Questions:
https://www.jsmount.com/angular-interview-question-answers-for-experienced-part-1/
JavaScript Interview Question..

  1. Hello to everybody, it’s my first go-to see of this weblog; this blog consists of awesome and genuinely fine data in favor of readers.

Leave a Reply

Your email address will not be published.