Fixed formatting problems

This commit is contained in:
Alexandrshy
2019-07-23 08:37:29 +04:00
parent bac32b613b
commit 84b6d19416
3 changed files with 156 additions and 159 deletions

View File

@@ -1,14 +1,18 @@
const voted = {}; const voted = {};
const checkVoter = (name) => {
/**
* Vote check
* @param {string} name Voter name
*/
const checkVoter = name => {
if (voted[name]) { if (voted[name]) {
console.log('kick them out!'); console.log("kick them out!");
} else { } else {
voted[name] = true; voted[name] = true;
console.log('let them vote!'); console.log("let them vote!");
} }
}; };
checkVoter("tom"); // let them vote!
checkVoter('tom'); // let them vote! checkVoter("mike"); // let them vote!
checkVoter('mike'); // let them vote! checkVoter("mike"); // kick them out!
checkVoter('mike'); // kick them out!

View File

@@ -1,16 +1,20 @@
'use strict'; "use strict";
const voted = {}; const voted = {};
/**
* Vote check
* @param {string} name Voter name
*/
function check_voter(name) { function check_voter(name) {
if (voted[name]) { if (voted[name]) {
console.log('kick them out!'); console.log("kick them out!");
} else { } else {
voted[name] = true; voted[name] = true;
console.log('let them vote!'); console.log("let them vote!");
} }
} }
check_voter("tom"); // let them vote! check_voter("tom"); // let them vote!
check_voter("mike"); // let them vote! check_voter("mike"); // let them vote!
check_voter("mike"); // kick them out! check_voter("mike"); // kick them out!

View File

@@ -1,173 +1,162 @@
/** /**
* Class HashTable * Class HashTable
* * @param {Object} obj
* @param {object} obj
*/ */
let HashTable = function( obj ) { const HashTable = function(obj) {
let length = 0; let length = 0;
this._items = ( function( obj ) { this._items = (function(obj) {
let items = {}; let items = {};
for ( let p in obj ) { for (let p in obj) {
items[p] = obj[p]; items[p] = obj[p];
length++; length++;
} }
return items; return items;
}( obj ) ); })(obj);
/** /**
* Associates the specified value to the specified key * Associates the specified value to the specified key
* * @param {string} key The key to which associate the value
* @param {string} key The key to which associate the value * @param {string} value The value to associate to the key
* @param {string} value The value to associate to the key * @returns {(undefined|Object)} Undefined is object didn't exists before this call
* */
* @return {(undefined|object)} Undefined is object didn't exists before this call this.set = function(key, value) {
*/ let previous = undefined;
this.set = function( key, value ) {
let previous = undefined;
if ( this.has( key ) ) { if (this.has(key)) {
previous = this._items[key]; previous = this._items[key];
} else { } else {
length++; length++;
} }
this._items[key] = value; this._items[key] = value;
return previous; return previous;
}; };
/** /**
* Returns the value associated to the specified key * Returns the value associated to the specified key
* * @param {string} key The key from which retrieve the value
* @param {string} key The key from which retrieve the value * @returns {(undefined|string)} Undefined or associated value
* */
* @return {(undefined|string)} Undefined or associated value this.get = function(key) {
*/ return this._items.hasOwnProperty(key) ? this._items[key] : undefined;
this.get = function( key ) { };
return this._items.hasOwnProperty( key ) ? this._items[key] : undefined;
};
/** /**
* Returns whether the hashtable contains the specified key * Returns whether the hashtable contains the specified key
* * @param {string} key The key to check
* @param {string} key The key to check * @returns {boolean}
* */
* @return {boolean} this.has = function(key) {
*/ return this._items.hasOwnProperty(key);
this.has = function( key ) { };
return this._items.hasOwnProperty( key );
};
/** /**
* Removes the specified key with its value * Removes the specified key with its value
* * @param {string} key The key to remove
* @param {string} key The key to remove * @returns {(undefined|string)} Undefined if key doesn't exist and
* * string (previous value) - value of deleted item
* @return {(undefined|string)} Undefined if key doesn't exist and */
* string (previous value) - value of deleted item this.remove = function(key) {
*/ if (this.has(key)) {
this.remove = function( key ) { let previous = this._items[key];
if ( this.has( key ) ) { length--;
let previous = this._items[key]; delete this._items[key];
length--; return previous;
delete this._items[key]; } else {
return previous; return undefined;
} else { }
return undefined; };
}
};
/** /**
* Returns an array with all the registered keys * Returns an array with all the registered keys
* * @returns {Array}
* @return {array} */
*/ this.getKeys = function() {
this.getKeys = function() { let keys = [];
let keys = [];
for ( let i in this._items ) { for (let i in this._items) {
if ( this.has( i ) ) { if (this.has(i)) {
keys.push( i ); keys.push(i);
} }
} }
return keys; return keys;
}; };
/** /**
* Returns an array with all the registered values * Returns an array with all the registered values
* * @returns {Array}
* @return {array} */
*/ this.getValues = function() {
this.getValues = function() { let values = [];
let values = [];
for ( let i in this._items ) { for (let i in this._items) {
if ( this.has( i ) ) { if (this.has(i)) {
values.push( this._items[i] ); values.push(this._items[i]);
} }
} }
return values; return values;
}; };
/** /**
* Iterates all entries in the specified iterator callback * Iterates all entries in the specified iterator callback
* @param {function} callback A method with 2 parameters: key, value * @param {function} callback A method with 2 parameters: key, value
*/ */
this.each = function( callback ) { this.each = function(callback) {
for ( let i in this._items ) { for (let i in this._items) {
if ( this.has( i ) ) { if (this.has(i)) {
callback( i, this._items[i] ); callback(i, this._items[i]);
} }
} }
}; };
/** /**
* Deletes all the key-value pairs on the hashmap * Deletes all the key-value pairs on the hashmap
*/ */
this.clear = function() { this.clear = function() {
this._items = {}; this._items = {};
length = 0; length = 0;
}; };
/** /**
* Gets the count of the entries in the hashtable * Gets the count of the entries in the hashtable
*/ */
Object.defineProperty( this, 'length', { Object.defineProperty(this, "length", {
get: function() { get: function() {
return length; return length;
}, }
}); });
/** /**
* Gets an array of all keys in the hashtable * Gets an array of all keys in the hashtable
*/ */
Object.defineProperty(this, 'keys', { Object.defineProperty(this, "keys", {
get: function() { get: function() {
return this.getKeys(); return this.getKeys();
}, }
}); });
/** /**
* Gets an array of all values in the hashtable * Gets an array of all values in the hashtable
*/ */
Object.defineProperty(this, 'values', { Object.defineProperty(this, "values", {
get: function() { get: function() {
return this.getValues(); return this.getValues();
}, }
}); });
}; };
let hashtable = new HashTable({'one': 1, 'two': 2, 'three': 3, 'cuatro': 4}); const hashtable = new HashTable({ one: 1, two: 2, three: 3, cuatro: 4 });
console.log( 'Original length: ' + hashtable.length ); // Original length: 4 console.log("Original length: " + hashtable.length); // Original length: 4
console.log( 'Value of key "one": ' + hashtable.get( 'one' ) ); // Value of key "one": 1 console.log('Value of key "one": ' + hashtable.get("one")); // Value of key "one": 1
console.log( 'Has key "foo"? ' + hashtable.has( 'foo' )); // Has key "foo"? false console.log('Has key "foo"? ' + hashtable.has("foo")); // Has key "foo"? false
console.log( 'Previous value of key "foo": ' + hashtable.set( 'foo', 'bar' ) ); // Previous value of key "foo": undefined console.log('Previous value of key "foo": ' + hashtable.set("foo", "bar")); // Previous value of key "foo": undefined
console.log( 'Length after set: ' + hashtable.length ); // Length after set: 5 console.log("Length after set: " + hashtable.length); // Length after set: 5
console.log( 'Value of key "foo": ' + hashtable.get( 'foo' ) ); // Value of key "foo": bar console.log('Value of key "foo": ' + hashtable.get("foo")); // Value of key "foo": bar
console.log( 'Value of key "cuatro": ' + hashtable.get( 'cuatro' )); // Value of key "cuatro": 4 console.log('Value of key "cuatro": ' + hashtable.get("cuatro")); // Value of key "cuatro": 4
console.log( 'Get keys by using property: ' + hashtable.keys ); // Get keys by using property: one,two,three,cuatro,foo console.log("Get keys by using property: " + hashtable.keys); // Get keys by using property: one,two,three,cuatro,foo
hashtable.clear(); hashtable.clear();
console.log( 'Length after clear: ' + hashtable.length ); // Length after clear: 0 console.log("Length after clear: " + hashtable.length); // Length after clear: 0