General
28-July-2024

Important Machine Coding Questions

JSDataStructureAlgorithmsDOM

Important Machine Coding Questions | Hero Image | s.mani.in
  1. Guess the output

    (function(){
        var a = b = 5
    })();
    
    console.log(typeof a);
    console.log(typeof b);
  2. Write an algorithm to validate whether the given HTML snippet is valid or not.

  3. Write an algorithm to fetch the latest package version from an array of versions, in string format, following semantic versionin rules (e.g. "a.b.c").

    const inputArray = ["5.4.1", "6.5.4", "1.4.3", "2.5.4"];
    // Expected output for the input array is "6.5.4"
  4. Guess the output -

    let vv = (function(x) {
      console.log(x)
      delete x;
      console.log(x)
      return x;
    })(0)
    
    console.log(vv)
  5. Make a network call to a publicly available API endpoint using -

    • fetch
    • XHR
    • node apis
  6. Write the code for a circular loading indicator.

  7. Create Instagram story like widget. The full story should be visible when you click on a story thumbnail, and it should go to the next story when the current one is finished.

  8. Guess the output of below mentioned program -

    for (var i = 1; i<10; i++) {
      setTimeout(() => console.log(i), 1000)
    }
    
    for (let j = 1; j<10; j++) {
      setTimeout(() => console.log(j), 1000)
    }
  9. Guess the output -

    const promise1 = new Promise((resolve, reject)=>{
      console.log(1);
      resolve('success');
    })
    
    promise1.then((data)=>{
      console.log(3);
    })
    
    console.log(4);
  10. Let's say you want to multiply 1,2,3,4..., n, where n represents positive integers. Write a generic method using currying in javscript.

    // if n=5, 
    // multiply(1)(2)(3)(4)(5)()
    
    // Output : 120
    Answer
    Arrow Down White
    function multiply(a) {
      let result = a;
    
      return function mult(b) {
        if (b) {
          result *= b;
          return mult;
        } else {
          return result;
        }
      };
    }
    
    console.log(multiply(1)(2)(3)(4)(5)());
  11. Guess the output -

    let X = {};
    let Y = { key: "hello" };
    let Z = { key: "world" };
    
    X[Z] = 200;
    X[Y] = 400;
    
    console.log(X[Z]);
  12. Guess the output -

    const someObj = {
        X: "foo",
        Y: function () {
            console.log(this.X);
        },
    };
      
    const Z = someObj.Y;
    
    someObj.Y();
    Z();
  13. Guess the output -

    console.log(typeof null);
    console.log(typeof undefined);
    console.log(null === undefined);
    console.log(null == undefined);
  14. Guess the output -

    const x = 10;
    
    function foo() {
      console.log(x);
      const x = 20;
    }
    
    foo();
  15. Guess the types -

    console.log(typeof 42);
    console.log(typeof "Hello");
    console.log(typeof true);
    console.log(typeof [1, 2, 3]);
    console.log(typeof { name: "John", age: 25 });
    console.log(typeof null);
    console.log(typeof undefined);
    console.log(typeof function () {});
    console.log(typeof NaN);
  16. What will be the output of the following code?

    const person = {
      name: "John",
      age: 30,
    };
    
    Object.freeze(person);
    person.age = 40;
    
    console.log(person.age);
  17. Guess the outputs -

    console.log([] == ![]);
    console.log("b" + "a" + +"a" + "a");
    console.log(NaN === NaN);
    console.log([1, 2, 3] + [4, 5, 6]);
    console.log([] + []);
    console.log([] + {});
    console.log({} + []);
    console.log(null == undefined);
    console.log(null === undefined);
    console.log([] == false);
    console.log([] === false);
    console.log(Object.is(NaN, NaN));
    console.log(NaN === NaN);
  18. Guess the output -

    const person = {
      name: "Alice",
      greet() {
        return () => console.log(`Hello, ${this.name}!`);
      },
    };
    
    const greet = person.greet();
    greet();
  19. Guess the output -

    async function mystery() {
      console.log(1);
      await Promise.resolve(console.log(2));
      console.log(3);
    }
    
    console.log(4);
    mystery();
    console.log(5);
  20. Guess the output -

    const arr = [10, 20, 30];
    arr.foo = 40;
    
    for (let i in arr) {
      console.log(i);
    }