Created
June 13, 2022 18:47
-
-
Save kevinmmartins/daa1c793bf27fc3cebe089cf35e94efe to your computer and use it in GitHub Desktop.
Simple map class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Implement a key-value store with get(key), put(key, value), delete(key) operations. | |
class CustomMap { | |
constructor() { | |
this._keys = []; | |
this._values = []; | |
}; | |
put(key,value) { | |
this._keys.push(key) | |
this._values.push(value) | |
} | |
_getPositionByKey (key) { | |
const position = this._keys.indexOf(key) | |
return position === -1 ? null : position | |
} | |
get(key) { | |
const position = this._getPositionByKey(key) | |
return this._values[position] | |
} | |
delete(key) { | |
const position = this._getPositionByKey(key) | |
delete this._keys[position] | |
delete this._values[position] | |
} | |
} | |
const map = new CustomMap() | |
map.put('kevin', 'uber') | |
map.put('nevil', 'uber2') | |
map.put('number', 124) | |
console.log(map.get('kevin')) | |
console.log(map.get('Null Test')) | |
console.log(map.get('nevil')) | |
console.log(map.get('number')) | |
map.delete("kevin") | |
console.log(map.get('nevil')) | |
console.log(map.get('kevin')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment