Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript maps #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/concepts/maps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* MAPS
* Maps are similar to javascript objects which holds key value pairs and remembers the original insertion order of keys.
*/

const map = new Map();

map.set('name', 'jhon doe'); // we use map.set() to insert values into the maps.
map.set('address', { city: 'hyderabad', place: 'Charminar' }); // can store objects inside maps
map.has('name'); // -> true (we use map.has() tocheck if a key is existing inside a map)
map.get('name'); // -> 'jhon doe' (we use map.get() to get a value of a key inside the map)
map.delete('address'); // we use map.delete() to delete a key and its value inside a map
console.log(map);
24 changes: 24 additions & 0 deletions src/problems/detectCapital.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

1) All letters in this word are capitals, like "USA".
2) All letters in this word are not capitals, like "leetcode".
3) Only the first letter in this word is capital, like "Google".
Otherwise, we define that this word doesn't use capitals in a right way.

Input: "USA"
Output: True

Input: "FlaG"
Output: False
*/

const detectCapitals = (word) => {
if (word.length === 0 || word.length === 1) return true;
if (word.toUpperCase() === word) return true; // case 'USA'
if (word.toLowerCase() === word) return true; // case 'leetcode'
if (word[0] + word.substring(1) === word) return true; // case 'Google'
return false;
};