Code Monkey home page Code Monkey logo

javascript-coding-challenges's Introduction

JavaScript-Coding-Challenges

JavaScript Coding Challenges

Recursion Coding Questions

  1. get all the names of the artists from the object into an array and finally return the array
const artistsByGenre = {
  jazz: ["Miles Davis", "John Coltrane"],
  rock: {
      classic: ["Bob Seger", "The Eagles"],
      hair: ["Def Leppard", "Whitesnake", "Poison"],
      alt: {
          classic: ["Pearl Jam", "The Killers"],
          current: ["Joywave", "Sir Sly"]
      }
  },
  unclassified: {
      new: ["Caamp", "Neil Young"],
      classic: ["Seal", "Morcheeba", "Chris Stapleton"]
  }
}

Answer:

const printArtist = (artistsObj,arr) => {
  if(artistsObj == null) return arr;
  for(let i in artistsObj){
    if(Array.isArray(artistsObj[i])){
       artistsObj[i].forEach((artist)=>{
         console.log(artist);
         arr.push(artist);
       });
    }else{
    // we do not return from here because it will break the loop so instead return the array after the loop
       printArtist(artistsObj[i],arr);
    }
  }
  return arr;
}


console.log('artists are', printArtist(artistsByGenre,[]));
  1. Deep copy of an object
Object.assign({}, obj);

// another way
JSON.parse(JSON.stringify(obj))
function deepCopy(obj){
  if(typeof obj == 'object' || obj == null) return obj;
  
  let newObj = Array.isArray(obj) ? [] : {};
  
  for(let i in obj){
    let value = obj[i];
    newObj[i] = deepCopy(value);
    }
    
    return newObj;
  }
  1. Create an object with a property 'marks' which cannot be set to a value less than 0
const normalObj = {
  marks:0,

  set setMarks(value){
    if(value < 0) throw new Error('Cant set value of marks less than zero');
    this.marks = value;
  },
  get getMarks(){
    return this.marks;
  }
}


normalObj.setMarks = 10;
console.log(normalObj.getMarks);
  1. Singleton Pattern
const singleton = (function(){
  function ProcessManager(){
    this.numprocess = 0;
  }

  let processManagerInstance = null;

  function createProcessManager(){
    
      processManagerInstance = new ProcessManager();
      return processManagerInstance;
  }
    return {

      getProcessManager: ()=>{
        if(!processManagerInstance){
          processManagerInstance = createProcessManager();
        }
        return processManagerInstance;
      }

    }
})()


let instanceOne = singleton.getProcessManager();
let instanceTwo = singleton.getProcessManager();

console.log(instanceOne === instanceTwo);
  1. Write a Prime function which accepts idenfinite numbers as argument and return result as array.

// func(1,3,5,8) and return [false,true,true,false]

const checkPrime = (...nums)=>{

  const result = nums.map((num)=> isPrime(num));
  return result;

}

const isPrime = (n)=>{

  if(n <= 1) return false;

  for(let i = 2; i <= n/2; i++){
    if(n%i==0) return false;
  }

  return true;
}

Now make a function which behaves like an api and send the result back after 3000 seconds

const asyncPrime = (nums,timeout)=>{
  return new Promise((resolve,reject)=>{
    setTimeout(()=> resolve(checkPrime(...nums)),timeout);
  })
}


asyncPrime([1,3,5,8],3000).then(console.log);

javascript-coding-challenges's People

Contributors

ghewadesumit avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.