Giter VIP home page Giter VIP logo

maps-set-exercise's Introduction

Maps and Set Exercise

Quick Question #1

What does the following code return?

new Set([1,1,2,2,3,4])

Solution #1

Return iterable object with uniques values

{
1,2,3,4
}

===

Quick Question #2

What does the following code return?

[...new Set("referee")].join("")

solution #2

return string

ref

===

Quick Questions #3

What does the Map m look like after running the following code?

let m = new Map();
m.set([1,2,3], true);
m.set([1,2,3], false);

solution #3

{
  [1,2,3]: true
  [1,2,3]: false
}

===

hasDuplicate

Write a function called hasDuplicate which accepts an array and returns true or false if that array contains a duplicate

hasDuplicate([1,3,2,1]) // true
hasDuplicate([1,5,-1,4]) // false

hasDuplicate Solution

function hasDuplicate(arr){
  return [...new Set(arr)].length === arr.length ? true : false
}

const hasDuplicate = arr => [..new Set(arr)].length === arr.length ? true : false

===

vowelCount

Write a function called vowelCount which accepts a string and returns a map where the keys are numbers and the values are the count of the vowels in the string.

vowelCount('awesome') // Map { 'a' => 1, 'e' => 2, 'o' => 1 }
vowelCount('Colt') // Map { 'o' => 1 }

vowelCount Solution

function vowelCount(str){
   return new Map(Object.entries(Array.from(str).filter(vow => "aeiou".indexOf(vow) > -1)
   .reduce((acc,next)=>{
     acc[next] = (acc[next] || 0) + 1
     return acc
   },{})))
}

function vowelCount(str){
  const myMap = new Map()

  for(let char of str){
    const lowerChar = char.toLowercase()
    if("aeiou".includes(lowerChar)){
      if(myMap.has(lowerChar)){
        myMap.set(lowerChar,myMap.get(lowerChar) + 1)
      }else{
        myMap.set(lowerChar,1)
      }
    }
  }

  return myMap
}

maps-set-exercise's People

Contributors

nickchow2020 avatar

Watchers

 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.