From 44e675a385647329cd4d47755cb2537f776fbb46 Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Thu, 13 Jul 2017 18:58:53 -0500 Subject: [PATCH 01/10] datatypes day 1 --- 01week/javascripting/hungergames.js | 27 ++++++++++ 01week/javascripting/introduction.js | 79 +++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 01week/javascripting/hungergames.js diff --git a/01week/javascripting/hungergames.js b/01week/javascripting/hungergames.js new file mode 100644 index 000000000..0b7c70383 --- /dev/null +++ b/01week/javascripting/hungergames.js @@ -0,0 +1,27 @@ +'use strict'; + +/** +* pick a ronadom student from this class +* +* store names in a variable- array +* +* generate a random number, less than amount in class +* +* apply the index to the array +* +* from that array, pick a random one +* +* return a name +* */ + +const studentArray = []; + +function randomNumberInRange (top, bottom){ + return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom; +} + +console.log(randomNumberInRange(14, 0)); +function generateRandomName() { + const index = randomNumberInRange(studentArray.length -1, 0); + return studentArray[index]; +} diff --git a/01week/javascripting/introduction.js b/01week/javascripting/introduction.js index e921523b1..95dd4be48 100644 --- a/01week/javascripting/introduction.js +++ b/01week/javascripting/introduction.js @@ -1 +1,78 @@ -console.log('hello'); +'use strict'; + +// Write a JavaScript program to display the current day and time. + +function currentDateTime(){ + console.log(new Date()); +} +currentDateTime(); + + +// Write a JavaScript program to convert a number to a string. + +function numberToString(num){ + console.log(num.toString()); +} +numberToString(10); + + + +// Write a JavaScript program to convert a string to the number. + +function stringToNumber(number){ + console.log(parseInt(number)); +} +stringToNumber(224); + + +// Write a JavaScript program that takes in different datatypes and prints out whether they are a: +// Boolean +// Null +// Undefined +// Number +// NaN +// String + +function ReturnType(someData){ + console.log(typeof someData); +} +ReturnType('Hello'); +ReturnType(10); +ReturnType(); +ReturnType(true); +ReturnType(); + + +// Write a JavaScript program that adds 2 numbers together. + +function addingStuff(dig1, dig2){ + console.log( dig1 + dig2 ); +} +addingStuff(4, 7); + +// Write a JavaScript program that runs only when 2 things are true. +function trueTings (x, y){ + if(x === true && y === true) { + console.log("It's True!"); + } +} +trueTings(true,true); + + +// Write a JavaScript program that runs when 1 of 2 things are true. + +function oneTingTrue (z, p) { + if (p || z === true) { + console.log("There can only be one"); + } +} +oneTingTrue(true, true); + +// Write a JavaScript program that runs when both things are not true. + +function noTrueTings (w, r) { + if (w != 2 && r != 13){ + console.log("Yeah right! Neither are true"); + } +} +noTrueTings(true, true); From eb446d2d1c9877c83673520369746a0fea6c0ca3 Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Thu, 13 Jul 2017 19:58:31 -0500 Subject: [PATCH 02/10] datatypes change --- 01week/javascripting/introduction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01week/javascripting/introduction.js b/01week/javascripting/introduction.js index 95dd4be48..3f3452414 100644 --- a/01week/javascripting/introduction.js +++ b/01week/javascripting/introduction.js @@ -40,7 +40,7 @@ ReturnType('Hello'); ReturnType(10); ReturnType(); ReturnType(true); -ReturnType(); +ReturnType(0/0); // Write a JavaScript program that adds 2 numbers together. From 648d94f7c26593112e02c44777365bfa593ea7eb Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Tue, 18 Jul 2017 18:17:25 -0500 Subject: [PATCH 03/10] rock paper scissors --- 01week/rockPaperScissors.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/01week/rockPaperScissors.js b/01week/rockPaperScissors.js index 16f58790a..af3606c24 100644 --- a/01week/rockPaperScissors.js +++ b/01week/rockPaperScissors.js @@ -7,17 +7,44 @@ const rl = readline.createInterface({ output: process.stdout }); +// My Code function rockPaperScissors(hand1, hand2) { - // Write code here + if (hand1 === hand2) { + return "It's a tie!"; + + } else if (hand1 === "Rock") { + + if (hand2 === "Paper") { + return "Hand 2 wins with Paper!"; + } else { + return "Hand 1 wins with Rock"; + } + + } else if (hand1 === "Paper") { + + if (hand2 === "Rock") { + return "Hand 1 wins with Paper!"; + } else { + return "Hand 2 wins with Scissors"; + } + + } else(hand1 === "Scissors"); { + + } + if (hand2 === "Rock") { + return "Hand 2 wins with Rock!"; + } else { + return "Hand one with Scissors"; + } } function getPrompt() { rl.question('hand1: ', (answer1) => { rl.question('hand2: ', (answer2) => { - console.log( rockPaperScissors(answer1, answer2) ); + console.log(rockPaperScissors(answer1, answer2)); getPrompt(); }); }); From 4c6d0a18dd4fbcd6cf9eb177e78fb851bd5106b4 Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Tue, 18 Jul 2017 19:48:48 -0500 Subject: [PATCH 04/10] Homework 1 --- 01week/javascripting/introduction.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/01week/javascripting/introduction.js b/01week/javascripting/introduction.js index 3f3452414..c05410a0e 100644 --- a/01week/javascripting/introduction.js +++ b/01week/javascripting/introduction.js @@ -3,9 +3,9 @@ // Write a JavaScript program to display the current day and time. function currentDateTime(){ - console.log(new Date()); + return(new Date()); } -currentDateTime(); +console.log(currentDateTime(); // Write a JavaScript program to convert a number to a string. @@ -13,7 +13,7 @@ currentDateTime(); function numberToString(num){ console.log(num.toString()); } -numberToString(10); +return numberToString(10); @@ -22,7 +22,7 @@ numberToString(10); function stringToNumber(number){ console.log(parseInt(number)); } -stringToNumber(224); +return stringToNumber(224); // Write a JavaScript program that takes in different datatypes and prints out whether they are a: From 7a97a3099430638c5d93e1174cc5b64318dd6f83 Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Fri, 21 Jul 2017 17:07:53 -0500 Subject: [PATCH 05/10] pig latin --- 02week/pigLatin.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 046434c94..3e1fbfea6 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -11,13 +11,45 @@ const rl = readline.createInterface({ function pigLatin(word) { // Your code here + // Array will be the word argument as an array + var array = word.split(''); + // Vowels going to test against + var vowels = ['a', 'e', 'i', 'o', 'u']; + + // Create newWord var to hold reordered letters + var newWord = ''; + + // Loop through leters in word + for (var y = 0; y < word.length - 1; y++) { + + // Loop through vowels + for (var i = 0; i < vowels.length - 1; i++) { + + + // If any letter in word matches a letter in vowwels + if (word[y] === vowels[i]) { + + + for (var x = y; x < word.length; x++) { + newWord = newWord + word[x]; + } + for (var n = 0; n < y; n++) { + newWord = newWord + word[n]; + } + return newWord + "ay"; + } + } + } } +translate("goat"); + + function getPrompt() { rl.question('word ', (answer) => { - console.log( pigLatin(answer) ); + console.log(pigLatin(answer)); getPrompt(); }); } From 24023441540e88aa7619a34e30bf1941491eae19 Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Tue, 25 Jul 2017 17:51:00 -0500 Subject: [PATCH 06/10] pig latin and array project1 --- 02week/arrayprjtct1.js | 106 ++++++++++++++++++++++++++++++++++++++++ 02week/pigLatin.js | 108 ++++++++++++++++++++++------------------- 2 files changed, 164 insertions(+), 50 deletions(-) create mode 100644 02week/arrayprjtct1.js diff --git a/02week/arrayprjtct1.js b/02week/arrayprjtct1.js new file mode 100644 index 000000000..d5f41992c --- /dev/null +++ b/02week/arrayprjtct1.js @@ -0,0 +1,106 @@ +'use strict'; + +// length +// Create an array called cars which consists of 4 different types of cars. The first car type should be Ford. +// Console.log the length of the array. +let cars = [ + 'Toyota', + 'Ford', + 'Audi', + 'VW' +]; +console.log(cars.length); + + +// concat +// Create another array called more cars with 4 more different types of cars. The last car type should be Honda. +// Use the concat method to combine the cars and moreCars arrays into an array called totalCars. +let moreCars = [ + 'BMW', + 'Ferrari', + 'Mercedez', + 'Honda' +]; +let totalCars = cars.concat(moreCars); +console.log(totalCars); + + +// indexOf and lastIndexOf +// Use the indexOf method to console.log the index of Honda. +// Use the lastIndexOf method to console.log the index of Ford. +console.log(totalCars.indexOf('Honda')); +console.log(totalCars.lastIndexOf('Ford')); + +// join +// Use the join method to covert the array totalCars into a string called stringOfCars. +let stringOfCars = totalCars.join(); +console.log(stringOfCars); + + +// split +// Use the split method to convert stringOfCars back intro an array called totalCars. +let totalCars2 = stringOfCars.split(); +console.log(totalCars2); + + +// reverse +// Use the reverse method to create an array carsInReverse which is the array totalCars in reverse. +let carsInReverse = totalCars.reverse(); +console.log(carsInReverse); +// An array of strings that is turned into a singular string, then turned back into an array is no longer an array of strings. +// Therefore we used the 1st array 'totalCars' of strings + + + +// sort +// Use the sort method to put the array carsInReverse into alphabetical order. +// Based on the types of cars you used, predict which item in the array should be at index 0. +// Use the following code to confirm or reject your prediction: +// alert(carsInReverse.indexOf('yourPrediction')); +let alphaCars = carsInReverse.sort(); +console.log(alphaCars); +alert(carsInReverse.indexOf('BMW')); + + +// slice +// Use the slice method to remove Ford and Honda from the carsInReverse array and move them into a new array called removedCars. +let removedCars = alphaCars.slice(3,5); +console.log(removedCars); + + +// splice +// Use the splice method to remove the 2nd and 3rd items in the array carsInReverse and add Ford and Honda in their place. +let carsSpliced = alphaCars.splice(1,2,"Ford", "Honda"); +console.log(carsSpliced); +console.log(alphaCars); + +// push +// Use the push method to add the types of cars that you removed using the splice method to the carsInReverse array. +let joinedCars = alphaCars.push('BMW', 'Ferrari'); +console.log(joinedCars); +console.log(alphaCars); + +// pop +// Use the pop method to remove and console.log the last item in the array carsInReverse. +console.log(carsInReverse.pop()); + + +// shift +// Use the shift method to remove and console.log the first item in the array carsInReverse. +console.log(carsInReverse.shift()); + + +// unshift +// Use the unshift method to add a new type of car to the array carsInReverse. +console.log(carsInReverse.unshift('Acura')); +console.log(carsInReverse); + + +// forEach +// Create an array called numbers with the following items: 23, 45, 0, 2 +// Write code that will add 2 to each item in the array numbers. +var numbers = [23, 45, 0 , 2]; +function addTwo(item) { + console.log(item + 2); +} +numbers.forEach(addTwo) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 3e1fbfea6..024a73ed7 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -11,73 +11,81 @@ const rl = readline.createInterface({ function pigLatin(word) { // Your code here - // Array will be the word argument as an array - var array = word.split(''); + // Declare pigLatin as a function and 'str' as a placeholder for a given string + const pigLatin = word => { - // Vowels going to test against - var vowels = ['a', 'e', 'i', 'o', 'u']; + // Make a list of vowels to test each letter of str against + let vowels = ['a', 'e', 'i', 'o', 'u']; - // Create newWord var to hold reordered letters - var newWord = ''; + // Turn the given string into an array and put it into 'result' var + let result = word.split(''); - // Loop through leters in word - for (var y = 0; y < word.length - 1; y++) { + // If the vowels array includes the first indexed letter of str + if (vowels.includes(word[0])) { - // Loop through vowels - for (var i = 0; i < vowels.length - 1; i++) { + // Return the given string with 'yay' added at the end + return word += 'yay'; + // The vowels array DOES NOT include the first indexed letter of str + } else { - // If any letter in word matches a letter in vowwels - if (word[y] === vowels[i]) { + // Loop over each letter in the string given + for (let i = 0; i < word.length; i++) { + // Using the iterator declared above, test each letter in str + // If the iterator DOES NOT include a letter in the vowel array + if (!vowels.includes(word[i])) { - for (var x = y; x < word.length; x++) { - newWord = newWord + word[x]; - } - for (var n = 0; n < y; n++) { - newWord = newWord + word[n]; + // Using str in array form, remove the first letter on the array, then add it to the end + result.push(result.shift()); + + // Once the iterator DOES match a letter in the vowel array + } else { + + // Add 'ay' to the end of the str + result.push('ay'); + + // Turn the rearranged str (var result) back into a string and return it + return result.join(''); } - return newWord + "ay"; } } } -} - -translate("goat"); - - -function getPrompt() { - rl.question('word ', (answer) => { - console.log(pigLatin(answer)); - getPrompt(); - }); -} -// Tests -if (typeof describe === 'function') { - describe('#pigLatin()', () => { - it('should translate a simple word', () => { - assert.equal(pigLatin('car'), 'arcay'); - assert.equal(pigLatin('dog'), 'ogday'); - }); - it('should translate a complex word', () => { - assert.equal(pigLatin('create'), 'eatecray'); - assert.equal(pigLatin('valley'), 'alleyvay'); + function getPrompt() { + rl.question('word ', (answer) => { + console.log(pigLatin(answer)); + getPrompt(); }); - it('should attach "yay" if word begins with vowel', () => { - assert.equal(pigLatin('egg'), 'eggyay'); - assert.equal(pigLatin('emission'), 'emissionyay'); - }); - it('should lowercase and trim word before translation', () => { - assert.equal(pigLatin('HeLlO '), 'ellohay'); - assert.equal(pigLatin(' RoCkEt'), 'ocketray'); + } + + // Tests + + if (typeof describe === 'function') { + + describe('#pigLatin()', () => { + it('should translate a simple word', () => { + assert.equal(pigLatin('car'), 'arcay'); + assert.equal(pigLatin('dog'), 'ogday'); + }); + it('should translate a complex word', () => { + assert.equal(pigLatin('create'), 'eatecray'); + assert.equal(pigLatin('valley'), 'alleyvay'); + }); + it('should attach "yay" if word begins with vowel', () => { + assert.equal(pigLatin('egg'), 'eggyay'); + assert.equal(pigLatin('emission'), 'emissionyay'); + }); + it('should lowercase and trim word before translation', () => { + assert.equal(pigLatin('HeLlO '), 'ellohay'); + assert.equal(pigLatin(' RoCkEt'), 'ocketray'); + }); }); - }); -} else { + } else { - getPrompt(); + getPrompt(); -} + } From 27105cd0d4c2eaf5e9d96e0571ebe48da000f4b6 Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Thu, 27 Jul 2017 17:53:10 -0500 Subject: [PATCH 07/10] towers of hanoi project --- 02week/ticTacToe.js | 11 +++++++---- 03week/partnerchallenge.js | 36 ++++++++++++++++++++++++++++++++++++ 03week/towersOfHanoi.js | 15 ++++++++++----- 3 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 03week/partnerchallenge.js diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 1abf5b900..62dee8b87 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -24,23 +24,26 @@ function printBoard() { } function horizontalWin() { - // Your code here + // if horizontal grid has equal items, there is a win + if (()) } function verticalWin() { - // Your code here + // if vertical grid has equal items, there is a win + } function diagonalWin() { - // Your code here + // if both diagonal grids have equal items, there is a win } function checkForWin() { - // Your code here + // } function ticTacToe(row, column) { // Your code here + board[row][column] = playerTurn; } function getPrompt() { diff --git a/03week/partnerchallenge.js b/03week/partnerchallenge.js new file mode 100644 index 000000000..70f949a9b --- /dev/null +++ b/03week/partnerchallenge.js @@ -0,0 +1,36 @@ +'use strict'; + +const partnerObj = { + firstName: 'Jdonathan', + lastName: 'Rowlitman', + age: 30, + location: 'Austin', + talk: function() { + console.log("Hello!"); + } +}; + + +console.log(partnerObj.firstName); + +console.log(partnerObj['firstName']); + +console.log(partnerObj.lastName); +partnerObj.talk(); +console.log(partnerObj); + + +partnerObj.location = 'cars'; +console.log(partnerObj); + + +const partnerArr = Object.keys(partnerObj); + +console.log(partnerArr); + +for (let i = 0; i < partnerArr.length; i++) { + partnerArr[i] + console.log(partnerArr[i]) + console.log(partnerObj[partnerArr[i]]) + +}; diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 165912ed8..04a8dee88 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -20,18 +20,23 @@ function printStacks() { } function movePiece() { - // Your code here + // move end of starting stack to available space + endStack.push(startStack.pop()); } function isLegal() { - // Your code here - + // if item is bigger than the piece in that field, it will not + // be able to land in that field + if (startStack[startStack.length - 1] < endStack[endStack.length -1]) { + return "Not an available move. Try again." +} else { + movePiece(); } function checkForWin() { - // Your code here - + // When stack equals to 4 in c field + endStack.length === 4; } function towersOfHanoi(startStack, endStack) { From 64467df741b6242431f6fff65a163c3678d2f149 Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Thu, 27 Jul 2017 20:11:30 -0500 Subject: [PATCH 08/10] working towers --- 03week/loops.js | 0 03week/towersOfHanoi.js | 49 ++++++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 03week/loops.js diff --git a/03week/loops.js b/03week/loops.js new file mode 100644 index 000000000..e69de29bb diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 04a8dee88..49fde201d 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -19,29 +19,48 @@ function printStacks() { console.log("c: " + stacks.c); } -function movePiece() { - // move end of starting stack to available space - endStack.push(startStack.pop()); - +function movePiece(startStack, endStack) { + // Take the last value in the startStack, add it to the end of the endStack. + stacks[endStack].push(stacks[startStack].pop()); } -function isLegal() { - // if item is bigger than the piece in that field, it will not - // be able to land in that field - if (startStack[startStack.length - 1] < endStack[endStack.length -1]) { - return "Not an available move. Try again." -} else { - movePiece(); +function isLegal(startStack, endStack) { + if (startStack && endStack){ + // Declare variables to hold the topmost piece in start and end stacks + const startIndex = stacks[startStack].length - 1; + const start = stacks[startStack][startIndex]; + const endIndex = stacks[endStack].length - 1; + const end = stacks[endStack].length ? stacks[endStack][endIndex]: 0; + + // If the piece to be moved is larger than the last piece on endStack, + console.log(start , end, start < end); + if (end === 0 || start < end) { + // If the piece to be moved is smaller than the last piece on endStack, the move is legal, + // Allow move + return 'legal move'; + } + } } function checkForWin() { - // When stack equals to 4 in c field - endStack.length === 4; + // If the length of either b or c stack is 4 + if (stacks.b.length === 4 || stacks.c.length === 4) { + // Return a You Win to the winner + return "You Win!"; + } } function towersOfHanoi(startStack, endStack) { - // Your code here - + // If the move is legal + if (isLegal(startStack, endStack)) { + // Move the piece + movePiece(startStack, endStack); + // Check for a win + checkForWin(); + } else { + console.log("Not a cool move! Choose another tower."); + return "Not a cool move! Choose another tower."; + } } function getPrompt() { From 9506a6ed91857ec7eef82fb504c2997ebaa7702e Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Tue, 1 Aug 2017 16:52:25 -0500 Subject: [PATCH 09/10] checkpoint complete --- 02week/ticTacToe.js | 2 +- 03week/loops.js | 77 ++++++++++++++++++++++++++++++++++++++++++++ 03week/mastermind.js | 42 +++++++++++++++++++++--- 3 files changed, 116 insertions(+), 5 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 62dee8b87..8a97940b1 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -25,7 +25,7 @@ function printBoard() { function horizontalWin() { // if horizontal grid has equal items, there is a win - if (()) + } function verticalWin() { diff --git a/03week/loops.js b/03week/loops.js index e69de29bb..9ae565309 100644 --- a/03week/loops.js +++ b/03week/loops.js @@ -0,0 +1,77 @@ +'use strict'; + +// for loop +// Use a for loop to console.log each item in the array carsInReverse. +const carsInReverse = [ + 'Audi', + 'BMW', + 'Ferrari', + 'Ford', + 'Honda', + 'Mercedez', + 'Toyota', + 'VW' ]; + +function showMeCars(listOfCars) { + for (let i = 0; i < listOfCars.length; i++) { + console.log(listOfCars[i]); + } +}; +showMeCars(carsInReverse); + +// for...in loop +// Create an object (an array with keys and values) called persons with the following data: +// firstName: "Jane" +// lastName: "Doe" +// birthDate: "Jan 5, 1925" +// gender: "female" + + + let persons = { + firstName: "Jane", + lastName: "Doe", + birthDate: "Jan 5, 1925", + gender: "female" +}; + +// Use a for...in loop to console.log each key. + + +for (key in persons) { + console.log(key); +} + + +// Then use a for...in loop and if state to console.log the value associated with the key birthDate. +for (key in persons) { + if (key === 'birthDate') { + console.log(persons['birthDate']); + } +}; + + +// while loop +// Use a for loop to console.log the numbers 1 to 1000. +let i = 0; +while (i < 1001) { + console.log(i); + i++ +}; + + +// do...while loop +// Use a do...while loop to console.log the numbers from 1 to 1000. +let num = 0; +do { + console.log(num); + num++ +} while (num < 1001) { +}; + + + + +// When is a for loop better than a while loop? +// How is the readability of the code affected? +// What is the difference between a for loop and a for...in loop? +// What is the difference between a while loop and a do...while loop? diff --git a/03week/mastermind.js b/03week/mastermind.js index 60e5cfa18..8bcf688a1 100644 --- a/03week/mastermind.js +++ b/03week/mastermind.js @@ -9,7 +9,7 @@ const rl = readline.createInterface({ let board = []; let solution = ''; -let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; +const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; function printBoard() { for (let i = 0; i < board.length; i++) { @@ -28,13 +28,47 @@ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } -function generateHint() { - // your code here +function generateHint(itemsArr) { + return `${itemsArr.length} are correct` + // } + function mastermind(guess) { solution = 'abcd'; // Comment this out to generate a random solution - // your code here + // The guess should contain 4 items which would compare each item position to + // the solution's items and position + // Create solution into an array in order to compare the index of each item + const solutionArr = solution.split(''); + // pushing the guess to the board to then check against solution + const boardLength = board.push(guess); + // if boardLength is equal to 4 compare to solution + const correctItemsArr = []; + + if (boardLength === 4) { + // console.log(solutionArr, 'arr') + + for (let i = 0; i < board.length; i++) { + + for (let e = 0; e < solutionArr.length; e++) { + + if (board[i] === solutionArr[e]) { + correctItemsArr.push(i) + } + } + } + if (correctItemsArr.length === 4) { + console.log('You are a BOSS! You guessed all 4 items correctly!') + return 'You are a BOSS! You guessed all 4 items correctly!' + } else { + console.log(generateHint(correctItemsArr)) + } + board = []; + } + + // if true then check for win(items and position are eqaul) + // if items and posiitons are not equal run hint + console.log(boardLength, 'length'); } From b66d0f04b14eafed2692967642a49cf272c4503b Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Tue, 1 Aug 2017 17:03:27 -0500 Subject: [PATCH 10/10] Working tic tac toe --- 02week/ticTacToe.js | 63 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 8a97940b1..d74bc137f 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -22,28 +22,64 @@ function printBoard() { console.log(' ---------'); console.log('2 ' + board[2].join(' | ')); } - +//code here function horizontalWin() { - // if horizontal grid has equal items, there is a win - + return board[0].every(square => square === playerTurn) || board[1].every(square => square === playerTurn) || board[2].every(square => square === playerTurn); } function verticalWin() { - // if vertical grid has equal items, there is a win - + return [board[0][0], board[1][0], board[2][0]].every(square => square === playerTurn) || [board[0][1], board[1][1], board[2][1]].every(square => square === playerTurn) || [board[0][2], board[1][2], board[2][2]].every(square => square === playerTurn); } function diagonalWin() { - // if both diagonal grids have equal items, there is a win + return [board[0][0], board[1][1], board[2][2]].every(square => square === playerTurn) || [board[0][2], board[1][1], board[2][0]].every(square => square === playerTurn); } function checkForWin() { - // + if (horizontalWin()) { + printBoard(); + console.log(`Congratulations player ${playerTurn}. You won on the horizontal!`); + return true; + } else if (verticalWin()) { + printBoard(); + console.log(`Congratulations player ${playerTurn}. You won on the vertical!`); + return true; + } else if (diagonalWin()) { + printBoard(); + console.log(`Congratulations player ${playerTurn}. You won on the diagonal!`); + return true; + } + return false; } function ticTacToe(row, column) { - // Your code here - board[row][column] = playerTurn; + + const validValue = (myIndex) => { + const valuesArr = [0,1,2]; + return valuesArr.some(validIndex => myIndex == validIndex); + } + + if (validValue(row) && validValue(column)) { + if (!board[row][column].trim() ) { + board[row][column] = playerTurn; + + if (!checkForWin()) { + if (playerTurn === 'X') { + playerTurn = 'O'; + } else { + playerTurn = 'X'; + } + return false; + } else { + console.log(`The winner is player ${playerTurn}. Start a new game`); + return true; + } + } else { + console.log('Please choose another square! That one is taken!'); + } + } else { + console.log('Please enter a valid index. Valid values are 0, 1, 2'); + } } function getPrompt() { @@ -51,15 +87,16 @@ function getPrompt() { console.log("It's Player " + playerTurn + "'s turn."); rl.question('row: ', (row) => { rl.question('column: ', (column) => { - ticTacToe(row, column); - getPrompt(); + if (!ticTacToe(row, column)) { + getPrompt(); + } else { + process.exit(0); + } }); }); } - - // Tests if (typeof describe === 'function') {