-
Notifications
You must be signed in to change notification settings - Fork 0
Checkpoint1 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
Checkpoint1 #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| 'use strict' | ||
|
|
||
| //length | ||
| let cars = ['Ford','Toyota','Cadillac','Hyundai']; | ||
| console.log(cars.length); | ||
|
|
||
| //concat | ||
| let moreCars = ['Aston Martin','Bugatti','Bentley','Honda']; | ||
| let totalCars = cars.concat(moreCars); | ||
| console.log(totalCars); | ||
|
|
||
| //indexOf | ||
| console.log(totalCars.indexOf("Honda")); | ||
|
|
||
| //lastIndexOf | ||
| console.log(totalCars.lastIndexOf("Ford")); | ||
|
|
||
| //join | ||
| let stringOfCars = totalCars.join(','); | ||
| console.log(stringOfCars); | ||
|
|
||
| //split | ||
| totalCars = stringOfCars.split(','); | ||
| console.log(totalCars); | ||
|
|
||
| //reverse | ||
| let carsInReverse = totalCars.reverse(); | ||
| console.log(carsInReverse); | ||
|
|
||
| //sort | ||
| carsInReverse.sort(); | ||
| alert(carsInReverse.indexOf('Aston Martin')); | ||
| console.log(carsInReverse); | ||
|
|
||
| //slice | ||
| let removedCars = carsInReverse.slice(4, 6); | ||
| console.log(removedCars); | ||
|
|
||
| //splice | ||
| carsInReverse.splice(1, 1, 'Ford'); | ||
| carsInReverse.splice(2, 1, 'Honda'); | ||
| console.log(carsInReverse); | ||
|
|
||
| //push | ||
| carsInReverse.push('Ford', 'Honda'); | ||
| console.log(carsInReverse); | ||
|
|
||
| //pop | ||
| carsInReverse.pop(); | ||
| console.log(carsInReverse); | ||
|
|
||
| //shift | ||
| carsInReverse.shift(); | ||
| console.log(carsInReverse); | ||
|
|
||
| //unshift | ||
| carsInReverse.unshift("Subaru"); | ||
| console.log(carsInReverse); | ||
|
|
||
| //forEach | ||
| var numbers = [23, 45, 0, 2]; | ||
| function addTwo(item) { | ||
| console.log(item + 2); | ||
| } | ||
| numbers.forEach(addTwo) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,66 @@ | ||
| 'use strict'; | ||
|
|
||
| /* | ||
| MASTERMIND | ||
|
|
||
| const assert = require('assert'); | ||
| const readline = require('readline'); | ||
| const rl = readline.createInterface({ | ||
| input: process.stdin, | ||
| output: process.stdout | ||
| }); | ||
|
|
||
| let board = []; | ||
| let solution = ''; | ||
| let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; | ||
|
|
||
| function printBoard() { | ||
| for (let i = 0; i < board.length; i++) { | ||
| console.log(board[i]); | ||
| } | ||
| } | ||
|
|
||
| - Start game by generating a four-letter code you want the player to guess (generatedCode) | ||
|
|
||
| function generatedCode() { | ||
| for (let i = 0; i < 4; i++) { | ||
| const randomIndex = getRandomInt(0, letters.length); | ||
| solution += letters[randomIndex]; | ||
| } | ||
| } | ||
|
|
||
| function getRandomInt(min, max) { | ||
| return Math.floor(Math.random() * (max - min)) + min; | ||
| } | ||
|
|
||
| -- The player chooses 4 letters (playerCode) | ||
|
|
||
| -- They win if they guess the code before using up all of their turns | ||
| if (playerCode === generatedCode) { | ||
| console.log('You win!'); | ||
| } else { | ||
| console.log('Keep trying'); | ||
| } | ||
|
|
||
| -- The letters are checked to see: | ||
| 1- if their code matches the generatedCode | ||
| 2- if any of the letters are correct | ||
| (correct letter) | ||
| 3- if they are also in the correct position | ||
| (correct position) | ||
| (Use 'loops', probably 'while' so can guess until generatedCode is true; or 'do while' if verifying playerCode and then running through rest of loop if incorrect...will see) | ||
|
|
||
| -- Alert the player of how many are the correct letter AND how many are in the correct position | ||
|
|
||
| -- The player loses if they use all of their turns and never guess the correct code | ||
|
|
||
| - Give 5 chances. Restart game when it's done. | ||
|
|
||
| -- Find a way to keep the players previous attempts and the results of those attempts on display | ||
| ---save their inputs into an array | ||
| ---show array on the screen | ||
| */ | ||
|
|
||
| const assert = require('assert'); | ||
| const readline = require('readline'); | ||
| const rl = readline.createInterface({ | ||
|
|
@@ -8,8 +69,10 @@ const rl = readline.createInterface({ | |
| }); | ||
|
|
||
| let board = []; | ||
| let solution = ''; | ||
| let generatedCode = ''; | ||
| let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; | ||
| let gameCounter=0; | ||
|
|
||
|
|
||
| function printBoard() { | ||
| for (let i = 0; i < board.length; i++) { | ||
|
|
@@ -20,43 +83,83 @@ function printBoard() { | |
| function generateSolution() { | ||
| for (let i = 0; i < 4; i++) { | ||
| const randomIndex = getRandomInt(0, letters.length); | ||
| solution += letters[randomIndex]; | ||
| generatedCode += letters[randomIndex]; | ||
| } | ||
| } | ||
|
|
||
| function getRandomInt(min, max) { | ||
| return Math.floor(Math.random() * (max - min)) + min; | ||
| } | ||
|
|
||
| function generateHint() { | ||
| // your code here | ||
| function mastermind(guess) { | ||
| if (guess === generatedCode) { | ||
| console.log('You win!'); | ||
| return; | ||
| } else { | ||
| console.log('Keep trying'); | ||
| } | ||
| generateHint(guess); | ||
| } | ||
|
|
||
| function mastermind(guess) { | ||
| solution = 'abcd'; // Comment this out to generate a random solution | ||
| // your code here | ||
|
|
||
| function generateHint(guess) { | ||
| let gueArray = guess.split(''); | ||
| let genArray = generatedCode.split(''); | ||
| let counter=0; | ||
|
|
||
| for(let i = 0; i < gueArray.length;i++) | ||
| { | ||
| for(let j=0; j < genArray.length;j++) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You only need one loop here. |
||
| { | ||
| if (gueArray[i]===genArray[j]) | ||
| counter+=1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could also use |
||
| } | ||
| } | ||
| console.log('you have '+counter+' correct letters'); | ||
|
|
||
| let positionCounter=0; | ||
| if (gueArray[0] === genArray[0]) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consolidate lines 121-132 using a for loop and |
||
| positionCounter+=1; | ||
| } | ||
| if (gueArray[1] === genArray[1]) { | ||
| positionCounter+=1; | ||
| } | ||
| if (gueArray[2] === genArray[2]) { | ||
| positionCounter+=1; | ||
| } | ||
| if (gueArray[3] === genArray[3]) { | ||
| positionCounter+=1; | ||
| } | ||
| console.log('you have '+positionCounter+' letters in the correct position'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return statement here showing correct letters and correct letter locations. |
||
| } | ||
|
|
||
|
|
||
| function getPrompt() { | ||
| rl.question('guess: ', (guess) => { | ||
| rl.question('guess 4 letters from A through H: ', (guess) => { | ||
| mastermind(guess); | ||
| //put the guesses in array | ||
| board.push(guess); | ||
| printBoard(); | ||
| gameCounter+=1; | ||
| if (gameCounter > 10) { | ||
| console.log("GAME OVER!!") | ||
| return; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove empty return statements from your code. |
||
| } | ||
| getPrompt(); | ||
| }); | ||
| } | ||
|
|
||
| // Tests | ||
|
|
||
| if (typeof describe === 'function') { | ||
| solution = 'abcd'; | ||
| generatedCode = 'abcd'; | ||
| describe('#mastermind()', () => { | ||
| it('should register a guess and generate hints', () => { | ||
| mastermind('aabb'); | ||
| assert.equal(board.length, 1); | ||
| }); | ||
| it('should be able to detect a win', () => { | ||
| assert.equal(mastermind(solution), 'You guessed it!'); | ||
| assert.equal(mastermind(generatedCode), 'You guessed it!'); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| 'use strict' | ||
|
|
||
| const partnerObj = { | ||
| firstName: "Renata", | ||
| lastName: "Estes", | ||
| age:27, | ||
| eyeColor: "brown", | ||
| talk: () => { | ||
| return "Hello Renee!" | ||
| }, | ||
| }; | ||
|
|
||
| partnerObj.lastName = "cars"; | ||
| console.log(partnerObj['firstName']); | ||
| console.log(partnerObj.lastName); | ||
| console.log(partnerObj.talk()); | ||
|
|
||
| const partnerArr = Object.keys(partnerObj); | ||
| console.log(partnerArr); | ||
|
|
||
| for (var i in partnerArr){ | ||
| console.log(partnerArr[i]) | ||
| console.log(partnerObj[partnerArr[i]]) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to return your "win" statement here, instead of just console logging here.
See line 161 to test instructions.