From 000d79639f8d410947cf2240911d21e355536b0e Mon Sep 17 00:00:00 2001 From: romeoaaron21 Date: Tue, 28 May 2019 15:58:42 +0800 Subject: [PATCH] Javascript-4-prototype Completed --- arrayProperty.js | 6 ++++- instanceArray.js | 26 ++++++++++++++++++++ quizApp.js | 45 +++++++++++++++++++++++++++++++++- sayName.js | 13 ++++++++++ test/spec/arrayPropertySpec.js | 4 +-- test/spec/quizAppSpec.js | 4 +-- test/spec/sayNameSpec.js | 4 +-- user.json | 4 +-- 8 files changed, 96 insertions(+), 10 deletions(-) diff --git a/arrayProperty.js b/arrayProperty.js index 2287a36..d0da60f 100644 --- a/arrayProperty.js +++ b/arrayProperty.js @@ -2,4 +2,8 @@ //Add a reverse method to the String 'class' so that every instance of String can call reverse and reverse itself. - //code here \ No newline at end of file + //code here + + String.prototype.reverse = function(){ + return this.split("").reverse().join(""); + } diff --git a/instanceArray.js b/instanceArray.js index ab752ac..a0f9ea6 100644 --- a/instanceArray.js +++ b/instanceArray.js @@ -16,26 +16,52 @@ var User = function(name, email, pw){ //code here + var users = []; + + + //Now create and push into your users array 3 separate instances of User using the data from above in that exact order //code here + var person1 = new User('Aodhan', 'aodhan@boom.camp', 'iLoveJS'); + + var person2 = new User('Greg', 'greg@boom.camp', 'iLovePython'); + + var person3 = new User('Oscar', 'oscar@boom.camp', 'iLoveSoccer'); + + users.push(person1); + users.push(person2) + users.push(person3); + + console.log('Aodhan\'s information is '); //Console.log all of Aodhan information //code here + console.log('Aodhan\'s information is ', person1); + console.log('Oscar\'s information is '); //Now console.log all of Oscars information //code here + console.log('Aodhan\'s information is ', person3); //Now create another instance of User using your own information and then add that to your users array. //code here + var person4 = new User('Romeo', 'romeo.lumibao@boom.camp', 'iLoveYou'); + + users.push(person4); + console.log('All my users names are '); //Now loop through your users Array and console.log every users name. //code here + + for(let i = 0; i <= users.length-1; i++){ + console.log(users[i]['name']); + } diff --git a/quizApp.js b/quizApp.js index 083bd6c..ffb0354 100644 --- a/quizApp.js +++ b/quizApp.js @@ -6,26 +6,50 @@ //Create a QuizUser constructor that accepts name, email, password, and totalScore parameters and set them appropriatly //code here - + function QuizUser(name, email, password, totalScore){ + this.name = name; + this.email = email; + this.password = password; + this.totalScore = totalScore; + } //Create a Question constructor that accepts title, answersArray, rightAnswer, and difficulty parameters //code here + function Question(title, answersArray, rightAnswer, difficulty){ + this.title = title; + this.answersArray = answersArray; + this.rightAnswer = rightAnswer; + this.difficulty = difficulty; + } + //Create a quizUsers Array which is going to hold all of our users. //code here + var quizUsers = []; + //Let's say three people signed up for our service, create 3 instances of User and add each to the users Array //code here + var person1 = new QuizUser('Person1', 'person1@user', 'password1', 25); + var person2 = new QuizUser('Person2', 'person2@user', 'password2', 29); + var person3 = new QuizUser('Person3', 'person3@user', 'password3', 30); + + quizUsers.push(person1); + quizUsers.push(person2); + quizUsers.push(person3); + //Create a questions Array which is going to hold all of our questions //code here + var questions = []; + //Now, let's say we wanted to create a quiz about JavaScript. Create three instances of Question which contain the following data @@ -36,13 +60,32 @@ //code here + var question1 = new Question('T/F: Inheritance is achieved in JavaScript through Prototypes?', [1,2,3,4], 'T', 'hard'); + var question2 = new Question('T/F: JavaScript is just a scripting version of Java', [1,2,3,4], 'T', 'hard'); + var question3 = new Question("T/F: In Javascript, == doesn't check 'type' but just the value - where === checks type and value", [1,2,3,4], 'T', 'hard'); + + //Now push all of your instances of Question into the questions Array //code here + questions.push(question1); + questions.push(question2); + questions.push(question3); + console.log('My users Array and my questions arrray are ...'); //Now loop console.log your users array and your questions array and verify that they're both holding the right data. //code here + for(let i = 0; i <= quizUsers.length-1; i++){ + console.log(quizUsers[i]); + } + + for(let j = 0; j <= questions.length-1; j++){ + console.log(questions[j]); + } + + + diff --git a/sayName.js b/sayName.js index 6e28b51..e3fd3e5 100644 --- a/sayName.js +++ b/sayName.js @@ -2,12 +2,25 @@ //code here + function Person(name, age){ + this.name = name; + this.age = age; + } + //Now create three instances of Person with data you make up //code here + var person1 = new Person('Romeo', 21); + var person2 = new Person('Aaron', 18); + var person3 = new Person('Ron', 14); + //Now add a sayName method on your Person class that will alert the name of whatever Person instance called it. //code here + + Person.prototype.sayName = function(){ + alert(this.name); + } diff --git a/test/spec/arrayPropertySpec.js b/test/spec/arrayPropertySpec.js index 6907a75..3969c12 100644 --- a/test/spec/arrayPropertySpec.js +++ b/test/spec/arrayPropertySpec.js @@ -1,10 +1,10 @@ -const isNode = new Function(` +/*const isNode = new Function(` try { return this === global; } catch (e) { return false; } -`); +`);*/ if (isNode()) { // test if file is running in a node process diff --git a/test/spec/quizAppSpec.js b/test/spec/quizAppSpec.js index 8e0c63c..dbe9d79 100644 --- a/test/spec/quizAppSpec.js +++ b/test/spec/quizAppSpec.js @@ -1,10 +1,10 @@ -const isNode = new Function(` +/*const isNode = new Function(` try { return this === global; } catch (e) { return false; } -`); +`);*/ if (isNode()) { // test if file is running in a node process diff --git a/test/spec/sayNameSpec.js b/test/spec/sayNameSpec.js index 7fbb7bb..bce5603 100644 --- a/test/spec/sayNameSpec.js +++ b/test/spec/sayNameSpec.js @@ -1,10 +1,10 @@ -const isNode = new Function(` +/*const isNode = new Function(` try { return this === global; } catch (e) { return false; } -`); +`);*/ if (isNode()) { // test if file is running in a node process diff --git a/user.json b/user.json index 4ac80a0..b1a41ce 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Romeo Aaron C. Lumibao I", + "email": "romeo.lumibao@boom.camp" }