diff --git a/arrayProperty.js b/arrayProperty.js index 2287a36..20268c3 100644 --- a/arrayProperty.js +++ b/arrayProperty.js @@ -1,5 +1,8 @@ +//Note: use var for declaring variables //Just like you can add methods to your own constructor, you can also add methods and properties to built in classes in JavaScript like Arrays and Objects. //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(""); +} \ No newline at end of file diff --git a/instanceArray.js b/instanceArray.js index ab752ac..908ecbc 100644 --- a/instanceArray.js +++ b/instanceArray.js @@ -1,3 +1,4 @@ +//Note: use var for declaring variables /* Your 3 Users will be the following. 0) Aodhan, aodhan@boom.camp, 'iLoveJS' @@ -5,37 +6,45 @@ 2) Oscar, oscar@boom.camp, 'iLoveSoccer' */ -var User = function(name, email, pw){ - this.name = name; - this.email = email; - this.pw = pw; +var User = function(name, email, pw) { + this.name = name; + this.email = email; + this.pw = 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 aodhan = new User('Aodhan', 'aodhan@boom.camp', 'iLoveJS'); +var greg = new User('Greg', 'greg@boom.camp', 'iLovePython'); +var oscar = new User('Oscar', 'oscar@boom.camp', 'iLoveSoccer'); + +users.push(aodhan); +users.push(greg); +users.push(oscar); console.log('Aodhan\'s information is '); //Console.log all of Aodhan information - //code here +console.log(users.aodhan); console.log('Oscar\'s information is '); //Now console.log all of Oscars information - //code here +console.log(users.oscar) //Now create another instance of User using your own information and then add that to your users array. - //code here +var koji = new User('Koji', 'rolando.adriano@boom.camp', 'iLoveBoomCamp'); +users.push(koji); console.log('All my users names are '); //Now loop through your users Array and console.log every users name. - //code here +for (var x of users) { + console.log(x) +} \ No newline at end of file diff --git a/javascript-4-prototype b/javascript-4-prototype new file mode 160000 index 0000000..e8c19e3 --- /dev/null +++ b/javascript-4-prototype @@ -0,0 +1 @@ +Subproject commit e8c19e345b6d4b4f09e7931baa8dae31811f2646 diff --git a/quizApp.js b/quizApp.js index 083bd6c..afe798e 100644 --- a/quizApp.js +++ b/quizApp.js @@ -1,3 +1,4 @@ +//Note: use var for declaring variables //We're going to create the JS for a basic quiz application. //Let's think about the nature of this quiz app first. We're going to be creating lots of user objects, and we're @@ -5,27 +6,42 @@ //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 +quizUsers.push(new QuizUser('one people', 'one@email.com', 'onepassword', 1)); +quizUsers.push(new QuizUser('two', 'two@mail.com', 'twopassword', 2)); +quizUsers.push(new QuizUser('three people', 'three@mail.com', 'threepassword', 3)); + //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 +50,23 @@ //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 +//code here +var firstQuestion = new Question('T/F: Inheritance is achieved in JavaScript through Prototypes?'); +var secondQuestion = new Question('T/F: JavaScript is just a scripting version of Java') +var thirdQuestion = new Question("T/F: In Javascript, == doesn't check 'type' but just the value - where === checks type and value"); //Now push all of your instances of Question into the questions Array - //code here +questions.push(firstQuestion, secondQuestion, thirdQuestion); 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 (var user_data of quizUsers) { + console.log(user_data); +} +questions.forEach(function(n) { + console.log(n); +}) \ No newline at end of file diff --git a/sayName.js b/sayName.js index 6e28b51..b05c54d 100644 --- a/sayName.js +++ b/sayName.js @@ -1,13 +1,21 @@ -//Create a Person constructor that accepts name and age as parameters and sets those properties accordingly in the Constructor. +//Note: use var for declaring variables - //code here +//Create a Person constructor that accepts name and age as parameters and sets those properties accordingly in the Constructor. +function Person(name, age) { + this.name = name; + this.age = age; +} //Now create three instances of Person with data you make up - //code here +var barbs = new Person('Elijah', '20'); +var jaako = new Person('Muhamad Jaako', '35'); + -//Now add a sayName method on your Person class that will alert the name of whatever Person instance called it. +//Now add a sayName method on your Person class that will log the name of whatever Person instance called it in the console. - //code here +Person.prototype.sayName = function(name) { + console.log(this.name) +} \ No newline at end of file diff --git a/test/spec/arrayPropertySpec.js b/test/spec/arrayPropertySpec.js index 6907a75..7682377 100644 --- a/test/spec/arrayPropertySpec.js +++ b/test/spec/arrayPropertySpec.js @@ -1,43 +1,38 @@ -const isNode = new Function(` - try { - return this === global; - } catch (e) { - return false; - } -`); -if (isNode()) { +var isNode = ( typeof window === 'undefined' ) + +if (isNode) { // test if file is running in a node process const fs = require('fs'); const path = require('path'); - const filePath = path.resolve(__dirname, '../../'); // this should be the root dir - fs.readdirSync(filePath) // eval all of the js files faking how a browser would execute - .filter(path => { - if (path) { - return /\.js$/.test(path); - } else { - return false; - } - }) - .forEach(file => { - global.eval(fs.readFileSync(`${filePath}/${file}`) + ''); - }); + const filePath = path.resolve(__dirname, '../../'); // this should be the root dir + fs.readdirSync(filePath) // eval all of the js files faking how a browser would execute + .filter(path => { + if (path) { + return /\.js$/.test(path); + } else { + return false; + } + }) + .forEach(file => { + global.eval(fs.readFileSync(`${filePath}/${file}`) + ''); + }); } // Test describe('arrayProperty', function() { - describe('String', function() { - it('should have a reverse method', function() { - var str = 'Hello'; - expect(str.reverse).toBeDefined(); - expect(str.reverse).toEqual(jasmine.any(Function)); - }); - describe('reverse method', function() { - it('should reverse the string', function() { - var str = 'Hello'; - expect(str.reverse()).toEqual('olleH'); - }); + describe('String', function() { + it('should have a reverse method', function() { + var str = 'Hello'; + expect(str.reverse).toBeDefined(); + expect(str.reverse).toEqual(jasmine.any(Function)); + }); + describe('reverse method', function() { + it('should reverse the string', function() { + var str = 'Hello'; + expect(str.reverse()).toEqual('olleH'); + }); + }); }); - }); -}); +}); \ No newline at end of file diff --git a/test/spec/instanceArraySpec.js b/test/spec/instanceArraySpec.js index 91e6170..bcd1df2 100644 --- a/test/spec/instanceArraySpec.js +++ b/test/spec/instanceArraySpec.js @@ -1,12 +1,5 @@ -const isNode = new Function(` - try { - return this === global; - } catch (e) { - return false; - } -`); - -if (isNode()) { +var isNode = ( typeof window === 'undefined' ) +if (isNode) { // test if file is running in a node process const fs = require('fs'); const path = require('path'); diff --git a/test/spec/quizAppSpec.js b/test/spec/quizAppSpec.js index 8e0c63c..d49498d 100644 --- a/test/spec/quizAppSpec.js +++ b/test/spec/quizAppSpec.js @@ -1,113 +1,107 @@ -const isNode = new Function(` - try { - return this === global; - } catch (e) { - return false; - } -`); +var isNode = ( typeof window === 'undefined' ) -if (isNode()) { +if (isNode) { // test if file is running in a node process const fs = require('fs'); const path = require('path'); - const filePath = path.resolve(__dirname, '../../'); // this should be the root dir - fs.readdirSync(filePath) // eval all of the js files faking how a browser would execute - .filter(path => { - if (path) { - return /\.js$/.test(path); - } else { - return false; - } - }) - .forEach(file => { - global.eval(fs.readFileSync(`${filePath}/${file}`) + ''); - }); + const filePath = path.resolve(__dirname, '../../'); // this should be the root dir + fs.readdirSync(filePath) // eval all of the js files faking how a browser would execute + .filter(path => { + if (path) { + return /\.js$/.test(path); + } else { + return false; + } + }) + .forEach(file => { + global.eval(fs.readFileSync(`${filePath}/${file}`) + ''); + }); } describe('quizApp', function() { - describe('QuizUser', function() { - it('should exist', function() { - expect(QuizUser).toBeDefined(); - }); - it('should be a function', function() { - expect(QuizUser).toEqual(jasmine.any(Function)); - }); - it('should return an object when called with new', function() { - var quizUser = new QuizUser('Tester', 'test', 'test', 0); - expect(quizUser).toEqual(jasmine.any(Object)); - }); - describe('return object', function() { - it('should have name, email, password, and totalScore properties', function() { - var quizUser = new QuizUser('Tester', 'test', 'test', 0); - expect(quizUser.hasOwnProperty('name')).toBe(true); - expect(quizUser.hasOwnProperty('email')).toBe(true); - expect(quizUser.hasOwnProperty('password')).toBe(true); - expect(quizUser.hasOwnProperty('totalScore')).toBe(true); - }); - }); - }); - describe('Question', function() { - it('should exist', function() { - expect(Question).toBeDefined(); - }); - it('should be a function', function() { - expect(Question).toEqual(jasmine.any(Function)); - }); - it('should return an object when called with new', function() { - var question = new Question('Tester', 'test', 'test', 0); - expect(question).toEqual(jasmine.any(Object)); - }); - describe('return object', function() { - it('should have title, answersArray, rightAnswer, and difficulty properties', function() { - var question = new Question('Tester', 'test', 'test', 0); - expect(question.hasOwnProperty('title')).toBe(true); - expect(question.hasOwnProperty('answersArray')).toBe(true); - expect(question.hasOwnProperty('rightAnswer')).toBe(true); - expect(question.hasOwnProperty('difficulty')).toBe(true); - }); - }); - }); - describe('quizUsers', function() { - it('should exist', function() { - expect(quizUsers).toBeDefined(); - }); - it('should be an array', function() { - expect(quizUsers).toEqual(jasmine.any(Array)); - }); - it('should have three items', function() { - expect(quizUsers.length).toBe(3); - }); - it('(when complete) should only contain instances of QuizUser', function() { - var areAllUsers = true; - var quizUsers = quizUsers || []; - for (var i = 0; i < quizUsers.length; i++) { - if (!users[i] instanceof QuizUser) { - areAllUsers = false; - } - } - expect(areAllUsers).toBe(true); - }); - }); - describe('questions', function() { - it('should exist', function() { - expect(questions).toBeDefined(); - }); - it('should be an array', function() { - expect(questions).toEqual(jasmine.any(Array)); - }); - it('should have three items', function() { - expect(questions.length).toBe(3); - }); - it('(when complete) should only contain instances of Question', function() { - var areAllQuestions = true; - var questions = questions || []; - for (var i = 0; i < questions.length; i++) { - if (!questions[i] instanceof Question) { - areAllQuestions = false; - } - } - expect(areAllQuestions).toBe(true); - }); - }); -}); + describe('QuizUser', function() { + it('should exist', function() { + expect(QuizUser).toBeDefined(); + }); + it('should be a function', function() { + expect(QuizUser).toEqual(jasmine.any(Function)); + }); + it('should return an object when called with new', function() { + var quizUser = new QuizUser('Tester', 'test', 'test', 0); + expect(quizUser).toEqual(jasmine.any(Object)); + }); + describe('return object', function() { + it('should have name, email, password, and totalScore properties', function() { + var quizUser = new QuizUser('Tester', 'test', 'test', 0); + expect(quizUser.hasOwnProperty('name')).toBe(true); + expect(quizUser.hasOwnProperty('email')).toBe(true); + expect(quizUser.hasOwnProperty('password')).toBe(true); + expect(quizUser.hasOwnProperty('totalScore')).toBe(true); + }); + }); + }); + describe('Question', function() { + it('should exist', function() { + expect(Question).toBeDefined(); + }); + it('should be a function', function() { + expect(Question).toEqual(jasmine.any(Function)); + }); + it('should return an object when called with new', function() { + var question = new Question('Tester', 'test', 'test', 0); + expect(question).toEqual(jasmine.any(Object)); + }); + describe('return object', function() { + it('should have title, answersArray, rightAnswer, and difficulty properties', function() { + var question = new Question('Tester', 'test', 'test', 0); + expect(question.hasOwnProperty('title')).toBe(true); + expect(question.hasOwnProperty('answersArray')).toBe(true); + expect(question.hasOwnProperty('rightAnswer')).toBe(true); + expect(question.hasOwnProperty('difficulty')).toBe(true); + }); + }); + }); + describe('quizUsers', function() { + it('should exist', function() { + expect(quizUsers).toBeDefined(); + }); + it('should be an array', function() { + expect(quizUsers).toEqual(jasmine.any(Array)); + }); + it('should have three items', function() { + expect(quizUsers.length).toBe(3); + }); + it('(when complete) should only contain instances of QuizUser', function() { + var areAllUsers = true; + var quizUsers = quizUsers || []; + for (var i = 0; i < quizUsers.length; i++) { + if (!users[i] instanceof QuizUser) { + areAllUsers = false; + } + } + expect(areAllUsers).toBe(true); + }); + }); + describe('questions', function() { + it('should exist', function() { + expect(questions).toBeDefined(); + }); + it('should be an array', function() { + expect(questions).toEqual(jasmine.any(Array)); + }); + it('should have three items', function() { + expect(questions.length).toBe(3); + }); + it('(when complete) should only contain instances of Question', function() { + var areAllQuestions = true; + var questions = questions || []; + for (var i = 0; i < questions.length; i++) { + if (!questions[i] instanceof Question) { + areAllQuestions = false; + } + } + expect(areAllQuestions).toBe(true); + }); + }); +}); \ No newline at end of file diff --git a/test/spec/sayNameSpec.js b/test/spec/sayNameSpec.js index 7fbb7bb..f86f291 100644 --- a/test/spec/sayNameSpec.js +++ b/test/spec/sayNameSpec.js @@ -1,12 +1,6 @@ -const isNode = new Function(` - try { - return this === global; - } catch (e) { - return false; - } -`); +var isNode = ( typeof window === 'undefined' ) -if (isNode()) { +if (isNode) { // test if file is running in a node process const fs = require('fs'); const path = require('path'); @@ -53,11 +47,11 @@ describe('sayName', function() { it('should alert the name of the person on which sayName is invoked', function() { var tester = new Person('Tester', 25); var someoneElse = new Person('Simone Elsa', 22); - var alert = spyOn(window, 'alert'); + var consoleLog = spyOn(console, 'log'); tester.sayName(); - expect(alert).toHaveBeenCalledWith('Tester'); + expect(consoleLog).toHaveBeenCalledWith('Tester'); someoneElse.sayName(); - expect(alert).toHaveBeenCalledWith('Simone Elsa'); + expect(consoleLog).toHaveBeenCalledWith('Simone Elsa'); }); }); }); diff --git a/user.json b/user.json index 4ac80a0..04e3ca5 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" -} + "name": "Rolando Koji E Adriano Jr", + "email": "rolando.adriano@boom.camp" +} \ No newline at end of file