Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Data-Structures/Map/Map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
//The code creates a Map named map1 and sets key-value pairs ('a': 1, 'b': 2, 'c': 3). It
//retrieves and logs the value associated with key 'a', then updates it to 97. It logs the map's
//size (number of pairs) before and after deleting key 'b'. Expected output: 1, 97, 3, 2.

const map1 = new Map();

map1.set('a', 1);
map1.set('b', 2);
map1.set('c', 3);

console.log(map1.get('a'));
// Expected output: 1

map1.set('a', 97);

console.log(map1.get('a'));
// Expected output: 97

console.log(map1.size);
// Expected output: 3

map1.delete('b');

console.log(map1.size);
// Expected output: 2
33 changes: 33 additions & 0 deletions Data-Structures/Map/Map.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe('Map Operations', () => {
let map1;

beforeEach(() => {
map1 = new Map();
map1.set('a', 1);
map1.set('b', 2);
map1.set('c', 3);
});

it('should get the correct value for key "a" initially', () => {
const valueA = map1.get('a');
expect(valueA).toBe(1);
});

it('should update the value for key "a" to 97', () => {
map1.set('a', 97);
const updatedValueA = map1.get('a');
expect(updatedValueA).toBe(97);
});

it('should have a size of 3 initially', () => {
const initialSize = map1.size;
expect(initialSize).toBe(3);
});

it('should have a size of 2 after deleting key "b"', () => {
map1.delete('b');
const updatedSize = map1.size;
expect(updatedSize).toBe(2);
});
});