Skip to content

Instantly share code, notes, and snippets.

@dosjota
Forked from plataforma-co/test.js
Last active November 2, 2017 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dosjota/97cfc301569ee66ddc97740e2d701a9c to your computer and use it in GitHub Desktop.
Save dosjota/97cfc301569ee66ddc97740e2d701a9c to your computer and use it in GitHub Desktop.
Code Quality Test (Choose one of the files below)
'use strict';
// This class is used for logins
class Login {
constructor(hash) {
this.sessions = [];
this.users = [];
this.passwords = [];
Object.keys(hash).map(user => this.registerUser(user, hash[user]))
}
// to search using idx and replace array element using splice
logout(user) {
let index = this.idx(user, this.sessions);
return index !== -1 && this.sessions.splice(index,1)
}
// Checks if user exists, looking with idx if it already exists
userExists(user) {
return this.idx(user, this.users) !== -1 ? true : false;
}
// Register user, validation of empty parameters and use push instead of indexing
registerUser(user ='', password = '') {
if (!this.userExists(user) && user != '' && password != ''){
this.users.push(user);
this.passwords.push(password);
return true;
} else {
return false;
}
}
// use splice instead of assigning null and using idx (indexOf) instead of filter to search
removeUser(user) {
if (this.userExists(user)) {
let index = this.idx(user, this.users);
return index !== -1 && this.users.splice(index,1) && this.passwords.splice(index,1)
} else {
return false;
}
}
// check if the user exists and get their position to then compare password
checkPassword(user, password) {
let index = this.idx(user, this.users);
return this.passwords[index] === password;
}
// check the existence of the user, obtain its position to verify existence, if old password is the same as registered is replaced by the new password
updatePassword(user, oldPassword, newPassword) {
let index = this.idx(user, this.users);
if (index !== -1){
if (this.passwords[index] === oldPassword) {
this.passwords[index] = newPassword;
return true;
}
} else {
return false;
}
}
// check if you have an active session otherwise log in by checking the user and password
login(user, password) {
if (this.idx(user, this.sessions) !== -1) {
console.info(`${user} is already logged in!!`);
return false;
} else {
return this.checkPassword(user, password) && this.sessions.push(user);
}
}
// to use indexOf to search inside the array
idx(element, array) {
return array.indexOf(element)
}
}
let registeredUsers = {
user1: 'pass1',
user2: 'pass2',
user3: 'pass3'
};
let login = new Login(registeredUsers);
login.registerUser('user4', 'pass4');
login.login('user4', 'pass4');
login.updatePassword('user3', 'pass3', 'pass5');
login.login('user3', 'pass5');
login.logout('user4');
login.logout('user3');
# This class is used for logins
class Login
attr_reader :sessions, :users, :passwords
# Receives a hash with usernames as keys and passwords as values
def initialize(hash)
@sessions = []
@users = []
@passwords = []
hash.map do |k,v|
@users = @users + [k]
@passwords = @passwords + [v]
end
end
def logout(user)
sessions.each_with_index do |session, i|
sessions[i] = nil if session == user
end
sessions.compact!
end
# Checks if user exists
def user_exists(user)
# Temp variable for storing the user if found
temp = ''
for i in users
if i == user
temp = user
end
end
exists = temp != '' && temp == user
exists
end
# Register user
def register_user(user, password)
last_index = users.size
users[last_index] = user
passwords[last_index] = password
end
def remove_user(user)
index = idx(user, users)
users[index] = nil
passwords[index] = nil
users.compact!
passwords.compact!
end
def check_password(user, password)
index = idx(user, users)
password_correct = passwords[index] == password
return password_correct
end
def update_password(user, old_password, new_password)
# First we check if the user exists
user_1 = ''
for i in users
if i == user
user_1 = user
end
end
if user_1 == user
index = idx(user, users)
if passwords[index] == old_password
passwords[index] = new_password
return true
end
end
return false
end
def login(user, password)
index = idx(user, users)
if passwords[index] == password
sessions << user
end
end
# Gets index of an element in an array
def idx(element, array)
cont=0
for i in array
return cont if i == element
cont += 1
end
return cont
end
end
registered_users = {
'user1' => 'pass1',
'user2' => 'pass2',
'user3' => 'pass3'
}
login = Login.new(registered_users)
login.register_user('user4', 'pass4');
login.login('user4', 'pass4');
login.update_password('user3', 'pass3', 'pass5');
login.login('user3', 'pass5');
login.logout('user4');
login.logout('user3');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment