Skip to content

Instantly share code, notes, and snippets.

@plataforma-co
Last active November 23, 2017 12:38
Show Gist options
  • Save plataforma-co/e2caad8722e5767d996b9721281bffac to your computer and use it in GitHub Desktop.
Save plataforma-co/e2caad8722e5767d996b9721281bffac 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(k => ({k, v: hash[k]})).map(e => {
this.users = this.users.concat([e.k]);
this.passwords = this.passwords.concat([e.v]);
});
}
logout(user) {
this.sessions.forEach((session, i) => {
if (session === user) {
this.sessions[i] = null;
}
});
this.sessions = this.sessions.filter(session => session !== null);
}
// Checks if user exists
userExists(user) {
// Temp variable for storing the user if found
let temp = '';
for (let i of this.users) {
if (i === user) {
temp = user;
}
}
let exists = (temp !== '' && temp === user);
return exists;
}
// Register user
registerUser(user, password) {
let lastIndex = this.users.length;
this.users[lastIndex] = user;
this.passwords[lastIndex] = password;
}
removeUser(user) {
let index = this.idx(user, this.users);
this.users[index] = null;
this.passwords[index] = null;
this.users = this.users.filter(user => user !== null);
this.passwords = this.passwords.filter(password => password !== null);
}
checkPassword(user, password) {
let index = this.idx(user, this.users);
let passwordCorrect = this.passwords[index] === password;
return passwordCorrect;
}
updatePassword(user, oldPassword, newPassword) {
// First we check if the user exists
let user1 = '';
for (let i of this.users) {
if (i === user) {
user1 = user;
}
}
if (user1 === user) {
let index = this.idx(user, this.users);
if (this.passwords[index] === oldPassword) {
this.passwords[index] = newPassword;
return true;
}
}
return false;
}
login(user, password) {
let index = this.idx(user, this.users);
if (this.passwords[index] === password) {
this.sessions.push(user);
}
}
// Gets index of an element in an array
idx(element, array) {
let cont=0;
for (let i of array) {
if (i === element) {
return cont;
}
cont += 1;
}
return cont;
}
}
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