From a74f460b6aea40d1a78efe8ce3a0a6e7511bc4aa Mon Sep 17 00:00:00 2001 From: lyzamaemirabete Date: Wed, 16 Oct 2019 16:50:40 +0800 Subject: [PATCH 1/6] javascript 4-prototypes --- arrayProperty.js | 4 +++- instanceArray.js | 22 ++++++++++++++-------- quizApp.js | 44 ++++++++++++++++++++++++++++++++------------ sayName.js | 15 ++++++++++++--- user.json | 4 ++-- 5 files changed, 63 insertions(+), 26 deletions(-) diff --git a/arrayProperty.js b/arrayProperty.js index 2287a36..3b12274 100644 --- a/arrayProperty.js +++ b/arrayProperty.js @@ -2,4 +2,6 @@ //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 + String.prototype.reverse = function(){ + return this.split("").reverse().join("") + } diff --git a/instanceArray.js b/instanceArray.js index ab752ac..8d1924c 100644 --- a/instanceArray.js +++ b/instanceArray.js @@ -13,29 +13,35 @@ var User = function(name, email, pw){ //Create an Array called 'users' that will store all our instances of User. - //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 user1 = new User ('Aodhan', 'aodhan@boom.camp', 'iLoveJS') ; +var user2 = new User ('Greg', 'greg@boom.camp', 'iLovePython'); +var user3 = new User ('Oscar', 'oscar@boom.camp', 'iLoveSoccer'); +users.push(user1) +users.push(user2) +users.push(user3) console.log('Aodhan\'s information is '); //Console.log all of Aodhan information - //code here +console.log(users[0]); console.log('Oscar\'s information is '); //Now console.log all of Oscars information - //code here +console.log(users[2]); //Now create another instance of User using your own information and then add that to your users array. - //code here +var user4 = new User ('Lyza', 'lyza@boom.camp', 'iLoveYou'); +users.push(users[4]); console.log('All my users names are '); //Now loop through your users Array and console.log every users name. - //code here +for (val of users){ + console.log(val.name) +} diff --git a/quizApp.js b/quizApp.js index 083bd6c..51dba1f 100644 --- a/quizApp.js +++ b/quizApp.js @@ -5,27 +5,40 @@ //Create a QuizUser constructor that accepts name, email, password, and totalScore parameters and set them appropriatly - //code here - +var QuizUser = function (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 - +var Question = function (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 quizUser1 = new QuizUser ('Martha', 'martha@boom.camp', 'iLoveDansyle', 3) ; +var quizUser2 = new QuizUser ('dansyle', 'dansyle@boom.camp', 'iLoveMartha', 2); +var quizUser3 = new QuizUser ('vince', 'vince@boom.camp', 'iLoveVince', 1); +quizUsers.push(quizUser1) +quizUsers.push(quizUser2) +quizUsers.push(quizUser3) //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 @@ -34,15 +47,22 @@ //title: "T/F: In Javascript, == doesn't check 'type' but just the value - where === checks type and value" //Fill in the rest of the required data as you see appropriate. - //code here - - +var question1 = new Question ('T/F: Inheritance is achieved in JavaScript through Prototypes?', 'T/F', true, 'easy') +var question2 = new Question ('T/F: JavaScript is just a scripting version of Java', 'T/F', true, 'medium') +var question3 = new Question ("T/F: In Javascript, == doesn't check 'type' but just the value - where === checks type and value", 'T/F', false, '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 (val of quizUsers){ + console.log(val) +} +for (val of questions){ + console.log(val) +} diff --git a/sayName.js b/sayName.js index 6e28b51..924aca1 100644 --- a/sayName.js +++ b/sayName.js @@ -1,13 +1,22 @@ //Create a Person constructor that accepts name and age as parameters and sets those properties accordingly in the Constructor. - //code here +var Person = function (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 ('Lyza Mirabete', 21); +var person2 = new Person ('Vince Ludovice', 19); +var person3 = new Person ('Dansyle Marbella', 21); //Now add a sayName method on your Person class that will alert the name of whatever Person instance called it. +//console.log(this)// +Person.prototype.sayName = function(){ + alert(this.name) +} + - //code here diff --git a/user.json b/user.json index 4ac80a0..206fa1f 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Lyza Mae Mirabete", + "email": "lyza.mirabete@boom.camp" } From 0b1de6b6cbacce1a40839ea086c31c24fc1d6824 Mon Sep 17 00:00:00 2001 From: lyzamaemirabete Date: Thu, 17 Oct 2019 11:29:11 +0800 Subject: [PATCH 2/6] Added specs --- instanceArray.js | 2 +- test/spec/arrayPropertySpec.js | 7 ------- test/spec/quizAppSpec.js | 7 ------- test/spec/sayNameSpec.js | 7 ------- 4 files changed, 1 insertion(+), 22 deletions(-) diff --git a/instanceArray.js b/instanceArray.js index 8d1924c..633b8e5 100644 --- a/instanceArray.js +++ b/instanceArray.js @@ -42,6 +42,6 @@ users.push(users[4]); console.log('All my users names are '); //Now loop through your users Array and console.log every users name. -for (val of users){ +for (val in users){ console.log(val.name) } diff --git a/test/spec/arrayPropertySpec.js b/test/spec/arrayPropertySpec.js index 6907a75..b68215d 100644 --- a/test/spec/arrayPropertySpec.js +++ b/test/spec/arrayPropertySpec.js @@ -1,10 +1,3 @@ -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..ecb9af5 100644 --- a/test/spec/quizAppSpec.js +++ b/test/spec/quizAppSpec.js @@ -1,10 +1,3 @@ -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..8b76117 100644 --- a/test/spec/sayNameSpec.js +++ b/test/spec/sayNameSpec.js @@ -1,10 +1,3 @@ -const isNode = new Function(` - try { - return this === global; - } catch (e) { - return false; - } -`); if (isNode()) { // test if file is running in a node process From b90025e67b94467e285ff906e1cda7b0d5e21bc2 Mon Sep 17 00:00:00 2001 From: lyzamaemirabete <56061631+lyzamaemirabete@users.noreply.github.com> Date: Thu, 17 Oct 2019 14:59:13 +0800 Subject: [PATCH 3/6] Update arrayPropertySpec.js --- test/spec/arrayPropertySpec.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/spec/arrayPropertySpec.js b/test/spec/arrayPropertySpec.js index b68215d..6907a75 100644 --- a/test/spec/arrayPropertySpec.js +++ b/test/spec/arrayPropertySpec.js @@ -1,3 +1,10 @@ +const isNode = new Function(` + try { + return this === global; + } catch (e) { + return false; + } +`); if (isNode()) { // test if file is running in a node process From e2cfcee09090cdf86928e36055cdb62a581fc8b4 Mon Sep 17 00:00:00 2001 From: lyzamaemirabete <56061631+lyzamaemirabete@users.noreply.github.com> Date: Thu, 17 Oct 2019 15:00:21 +0800 Subject: [PATCH 4/6] Update instanceArraySpec.js From da7861c5c16578200122cfdf4e4d0dee7d79ce26 Mon Sep 17 00:00:00 2001 From: lyzamaemirabete <56061631+lyzamaemirabete@users.noreply.github.com> Date: Thu, 17 Oct 2019 15:02:05 +0800 Subject: [PATCH 5/6] Update quizAppSpec.js --- test/spec/quizAppSpec.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/spec/quizAppSpec.js b/test/spec/quizAppSpec.js index ecb9af5..8e0c63c 100644 --- a/test/spec/quizAppSpec.js +++ b/test/spec/quizAppSpec.js @@ -1,3 +1,10 @@ +const isNode = new Function(` + try { + return this === global; + } catch (e) { + return false; + } +`); if (isNode()) { // test if file is running in a node process From a53c9e58b20ff56964bb19e0a71c86d74cd76bd8 Mon Sep 17 00:00:00 2001 From: lyzamaemirabete <56061631+lyzamaemirabete@users.noreply.github.com> Date: Thu, 17 Oct 2019 15:02:50 +0800 Subject: [PATCH 6/6] Update sayNameSpec.js --- test/spec/sayNameSpec.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/spec/sayNameSpec.js b/test/spec/sayNameSpec.js index 8b76117..7fbb7bb 100644 --- a/test/spec/sayNameSpec.js +++ b/test/spec/sayNameSpec.js @@ -1,3 +1,10 @@ +const isNode = new Function(` + try { + return this === global; + } catch (e) { + return false; + } +`); if (isNode()) { // test if file is running in a node process