Apply for Zend Framework Certification Training

Javascript



< First Mysql Questions Part 2 >



 
Javascript interview questions
 
1) create a function to  reverse a string without using the built-in reverse() method.  
Ans 1)
function reverseString(str) { 
  let reversed = ‘’; 
  for (let i = str.length – 1; i >= 0; i–) { 
    reversed += str[i];
  } 
  return reversed; 
 
2) write a function to find the largest and smallest numbers in the array
a = [33,22,44,55,222,444,8]
    temp= a[0];
    for(i=1;i
        if(temp < a[i]){
            temp = a[i]
        }
    }
    console.log(temp)
 
3) what is an array and how it is different from json
 
4) difference between let and const
 
5) what is promises Explain with examples
 
6) write a  function that removes duplicates from an array, keeping only the unique elements
 
Write a program to convert from camel case to kebab case?
 
 
11.Function to remove first word from a string
 
Ans 11
 
function removeFirstWord() {
  //let str = 'Hello World';
    let str = 'public\dr1.jpg'
  //let result = str.substr(str.indexOf(' ')+1);
    let result = str.substr(str.indexOf('c')+1);
    return result;
}
 
removeFirstWord();
 
12 How to find length of an array with out using .length method
 
let myarray = [10,20,30,40,50,60,70,80]
const mylengthfunction = (myarray)=>{
    for(i=0;;i++){
        if(myarray[i]== undefined){
            break;
        }
    }
    return i
}
var length = mylengthfunction(myarray)
 
8) How to find the middle value index of let x =[22,80,34,80,80,11,55,80,80]
let a=[20,80,40,80,60,80,34,80,66,80,22,67,80,22,80]
i=0;
for(x in a){
    //console.log(x)
    if(a[x] == 80){
       //console.log(x)
       i++
    }
}
 if(i%2==0){
    checkmiddlevalue = false
}else{
    checkmiddlevalue = true
}
if(checkmiddlevalue == true){
        var middlevalue = (i/2)+.5;
        j=0;
        for(x in a){
            if(a[x] == 80){
            ++j;
              if(j == middlevalue){
                    console.log(x)
                    break;
              }            
            }
        }
}else{
    console.log("cannot find middle value ")
}
 
12) JavaScript Program to Remove Non-Alphanumeric Characters from a String
13) Bubble sort Implementation using JavaScript
 
function bubbleSort(arr) {
    for (var i = 0; i < arr.length; i++) {
        for (var j = 0; j < (arr.length - i - 1); j++) {
            if (arr[j] > arr[j + 1]) {
                var temp = arr[j]
                arr[j] = arr[j + 1]
                arr[j + 1] = temp
            }
        }
    }
    // Print the sorted array
    console.log(arr);
}
 
 
 
// This is our unsorted array
var arr = [234, 43, 55, 63, 5, 6, 235, 547];
// Now pass this array to the bblSort() function
bblSort(arr);
14) how to reverse an array elements
let x= [121,31,141,251,61];
let y = [];
let count = 0;
for(i=x.length-1;i>=0;i--){
    y[count] = x[i];
    count++;
}
 
console.log(x)
console.log(y)
7) create a  function that finds the second smallest element in an array of integers. 
 
15) How to get all matched value index in an string
 
let myarray = [10,40,20,30,40,50,40,60,40,70,40,60];
count = 0;
allmatchedelements = ""
for(i=0;i
    if(myarray[i] == 40){
       // console.log(i)
        allmatchedelements = allmatchedelements+i+' ';
        count++;
    }
}
console.log(allmatchedelements)
 
16) Sorting algorithm
let x = [211,45,2,9,666,23]
//console.log(x);
// if(x[0]>x[1]){
//     temp = x[0];
//     x[0] = x[1];
//     x[1] = temp;
// }
console.log(x);
for(i=x.length-1;i>=0;i--){
    for(j=0;j<=i;j++){
        if(x[j]>x[j+1]){
            temp = x[j];
            x[j] = x[j+1];
            x[j+1] = temp;
        }
    }
}
console.log(x);
 
17) given a string let mystring =”Hello how are you hello there and hello world”
Count the number of hello word repeated in an string
Ans 
 
let newmystringarray = mystring.split(' ')
console.log(newmystringarray)
count = 0;
for(i=0;i<=newmystringarray.length; i++){
    if(newmystringarray[i] == 'hello'){
        count++;
    }
}
console.log(count)

 

< First Mysql Questions Part 2 >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top