From 517043e4ec3e7c2d7a34f49afdfa825c4a330750 Mon Sep 17 00:00:00 2001 From: Jude Otenyo Date: Wed, 11 Jul 2018 02:34:06 +0300 Subject: [PATCH 1/2] INFO:: First part of the assignment in JavaScript --- README.md | 4 ++-- judeOtenyo.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 judeOtenyo.js diff --git a/README.md b/README.md index 3f78c7a..4ff8cbd 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ # Part 1 -In your favourite language, create an abstract data type representing a class( School class) +In your favorite language, create an abstract data type representing a class( School class) Name it Darasa. Darasa should be constructed using a list of strings representing class members. Darasa should have methods with specifications as described: @@ -18,7 +18,7 @@ Function specifications have been listed above the methods to be implemented. adds a new member into the Darasa. This method does not allow for duplicate Darasa members. If this member exists, handle it as you please. -arguements: String name +arguments: String name returns: Darasa member added or something else if member exists ''' def addClassMember(name): diff --git a/judeOtenyo.js b/judeOtenyo.js new file mode 100644 index 0000000..42c6afd --- /dev/null +++ b/judeOtenyo.js @@ -0,0 +1,44 @@ +export class Darasa { + constructor(name, size) { + this.name = name; + this.size = size; + this.wanafunzi = Array("Jude"); + } + + addClassMember(enteredStudentName) { + this.wanafunzi.forEach(student => { + if (student === enteredStudentName) { + return { + result: false, + message: 'A student with this name already exists' + }; + } + }); + + this.wanafunzi.push(enteredStudentName); + return { + result: true, + message: 'The student was added successfully' + }; + } + + removeClassMember(enteredStudentName) { + for (let i = 0; i < this.wanafunzi.length; i++) { + if (this.wanafunzi[i] === enteredStudentName) { + removedName = this.wanafunzi.splice(i, 1); + return { + result: true, + message: '${removedName} was removed successfully from ${this.name}' + }; + } + } + return { + result: false, + message: 'No such student exists in this class' + }; + } + + getDarasaMembers() { + return this.wanafunzi; + } +} \ No newline at end of file From 2660c46fd8247675c6765c8f1f7f299afdf74145 Mon Sep 17 00:00:00 2001 From: Jude Otenyo Date: Wed, 11 Jul 2018 03:52:11 +0300 Subject: [PATCH 2/2] INFO:: Commit after finishing the second part of the test --- README.md | 6 ++-- Shule.js | 60 ++++++++++++++++++++++++++++++++++++ judeOtenyo.js | 85 +++++++++++++++++++++++++++------------------------ 3 files changed, 108 insertions(+), 43 deletions(-) create mode 100644 Shule.js diff --git a/README.md b/README.md index 4ff8cbd..392cb48 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ def addClassMember(name): ''' removes member from this Darasa. If member does not exist in the Darasa, handle it as you please. -Arguements: String name +Arguments: String name returns: Darasa member removed from class or something else if member does not exist ''' def removeClassMember(name): @@ -76,7 +76,7 @@ Function specifications have been listed above the methods to be implemented. ''' adds a darasa to the school Registy returns the modified dictionary representation of the school. -arguements: String darasaName, Darasa darasa +arguments: String darasaName, Darasa darasa returns Dictionary representation of school ''' def addDarasa(name, darasa): @@ -87,7 +87,7 @@ def addDarasa(name, darasa): ''' removes a darasa entry from the school Registry. -arguements: String DarasaName +arguments: String DarasaName returns: modified Dictionary representation of school ''' def removeDarasa(name): diff --git a/Shule.js b/Shule.js new file mode 100644 index 0000000..2684c64 --- /dev/null +++ b/Shule.js @@ -0,0 +1,60 @@ +// var Darasa = require('./judeOtenyo'); +import Darasa from './judeOtenyo'; + +standard1 = new Darasa('standard1', 10, ['Jude', 'Joe', 'Jeff', 'Jane', 'Juliet', 'Joan']); +standard2 = new Darasa('standard2', 10, ['Amos', 'Aaron', 'Andrew', 'Ann', 'Abby', 'Ashley']); +standard3 = new Darasa('standard3', 10, ['Simon', 'Shaun', 'Samson', 'Sharon', 'Sarah', 'Sheila']); + + +class Shule { + constructor(schoolName) { + this.name = schoolName; + this.registry = Array(); + } + + addDarasa(name, darasa) { + this.registry.push({ name: name, darasa: darasa }); + return this.registry; + } + + removeDarasa(name) { + for (let i = 0; i < this.registry.length; i++) { + if (this.registry[1].name === name) { + removedDarasa = this.registry.splice(i, 1); + } + } + return this.registry; + } + + getShule() { + const currentShule = { shuleName: this.name, darasas: this.registry }; + return currentShule; + } + + //For doing my something interesting + getDarasa(darasaName) { + this.registry.forEach(darasa => { + if (darasaName === darasa.name) { + return darasa.darasa; + } + }); + } +} + +myAwesomeShule = new Shule('myAwesomeShule'); + +myAwesomeShule.addDarasa('standard1', standard1); +myAwesomeShule.addDarasa('standard2', standard2); +myAwesomeShule.addDarasa('standard3', standard3); + +//Something Interesting: Switching someones classes +function moveStudent(studentName, oldClass, newClass) { + result = myAwesomeShule.getDarasa(oldClass).removeClassMember(studentName); + if (result.result === true) { + myAwesomeShule.getDarasa(newClass).addClassMember(result.data); + } else { + alert(result.message); + } +} +//initializing the interesting thing +moveStudent('Jude', 'standard1', 'standard2'); \ No newline at end of file diff --git a/judeOtenyo.js b/judeOtenyo.js index 42c6afd..f9ef46e 100644 --- a/judeOtenyo.js +++ b/judeOtenyo.js @@ -1,44 +1,49 @@ -export class Darasa { - constructor(name, size) { - this.name = name; - this.size = size; - this.wanafunzi = Array("Jude"); - } + class Darasa { + constructor(name, size, students) { + this.name = name; + this.size = size; + this.wanafunzi = students; + } - addClassMember(enteredStudentName) { - this.wanafunzi.forEach(student => { - if (student === enteredStudentName) { - return { - result: false, - message: 'A student with this name already exists' - }; - } - }); + addClassMember(enteredStudentName) { + this.wanafunzi.forEach(student => { + if (student === enteredStudentName) { + return { + result: false, + data: '', + message: 'A student with this name already exists' + }; + } + }); - this.wanafunzi.push(enteredStudentName); - return { - result: true, - message: 'The student was added successfully' - }; - } + this.wanafunzi.push(enteredStudentName); + return { + result: true, + data: enteredStudentName, + message: 'The student was added successfully' + }; + } - removeClassMember(enteredStudentName) { - for (let i = 0; i < this.wanafunzi.length; i++) { - if (this.wanafunzi[i] === enteredStudentName) { - removedName = this.wanafunzi.splice(i, 1); - return { - result: true, - message: '${removedName} was removed successfully from ${this.name}' - }; - } - } - return { - result: false, - message: 'No such student exists in this class' - }; - } + removeClassMember(enteredStudentName) { + for (let i = 0; i < this.wanafunzi.length; i++) { + if (this.wanafunzi[i] === enteredStudentName) { + removedName = this.wanafunzi.splice(i, 1); + return { + result: true, + data: removedName, + message: '${removedName} was removed successfully from ${this.name}' + }; + } + } + return { + result: false, + data: '', + message: 'No such student exists in ${this.name}' + }; + } - getDarasaMembers() { - return this.wanafunzi; - } -} \ No newline at end of file + getDarasaMembers() { + return this.wanafunzi; + } + } + module.exports = Darasa; \ No newline at end of file