Skip to content
74 changes: 74 additions & 0 deletions 03week/inClassProject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

console.log('Project 2 - Loops');
console.log('');
console.log('');


// Test 1 - For loop
console.log('Test 1 - for loop');
const carsInReverse = ['Ford','Chevy','Buick','Chrysler','Nissan', 'Toyota'];
for (let i = 0; i< carsInReverse.length; i++) {
console.log(carsInReverse[i]);
}
console.log('');

// Test 2 - for... in loop.
console.log('Test 2 - For ... in loop');
const persons = {
firstName:'Jane',
lastName:'Doe',
birthDate:'Jan 5, 1925',
gender:'female'
};

for (const key in persons) {
console.log(key);
// console.log(`${key}: ${persons[key]}`);
}

for (let key in persons) {
if (key === 'birthDate') {
console.log(`She was born on ${persons[key]}`);
}
}
console.log('');

// Test 3 - while loop
console.log('Test 3 - using while loops');
let i = 1;
let numList = ''
while (i <= 1000) {
numList += i + ', ';
i++;
}
console.log(numList);
console.log('');

// Test 4 - Using do..while loop
console.log('Test 4 - using do..while loop');
i = 1;
numList = ''
do {
numList += i + ', ';
i++;
} while (i <= 1000);
console.log(numList);
console.log('');

// Test 5 - For vs while
console.log('Test 5 - For vs while loops');
console.log('When is a for loop better than a while?\nWell... when you are looping over a known number of items, like in an array');
console.log('How is the readability affected?\nFor loops define start value, condition to check, and iteration\nWhile loops only define the condition to check');
console.log('');

// Test 6 - for vs for..in loop
console.log('Test 6 - For vs for..in loop');
console.log('What is the difference between a for loop and a for..in loop\nThe for..in loop is a special loop for objects. One can loop on the keys of the object');
console.log('');


// Test 7 - While vs do..while
console.log('Test 7 - While vs do..while loop');
console.log('What is the difference between a while loop and do..while loop\nThe while loop checks the condition at the beginning of the loop.\nThe do..while loop, goes through the loop at least once, and checks the condition at the end.');
console.log('');
138 changes: 129 additions & 9 deletions 03week/mastermind.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
'use strict';
/******************************** WHITE BOARD NOTES ****************************************
// Mastermind... There is a set of items (letters, numbers, colors, shapes, etc.), the Gamemaster selects items from this set
// and places them in a certain order. The goal is for you to guess the right items in the right order.
// The Gamemaster (the one that knows the right combination), gives you 2 hints after each guess.
// a) right item, right place
// b) right item, wrong place.
// if it is a wrong item (it is also by definition in the wrong place), so it is not counted.
// You can use these hints to improve on your guess.
// You continue guessing and receiving hints until you get the right combination.
//
// For our computer game, the items are letters... a-h.
// The Computer is Gamemaster. It will randomly select 4 letters in a certain combination. This is the winning combination
// I will make a guess. Need to test to make sure player only select letters a-h. Need to test that player only pick 4.
// Computer will test for win. If the player won, announce winner. End game.
// If player didn't win, Computer will tell player how many are "right item right order" - "right item wrong order". It will look like 1-3, 2-1, etc.
// The player will guess again.
//
// ********************************** PSEUDO CODE *************************************
// generateSolution() // Given. Computer selects winning combination (combo of 4 letters)
// if valid entry(guess) then // Test for a) letters a-h, b) guess.length = solution.length (just 4 letters)
// if guess = solution then
// Announce winner and number of guesses it took!
// End game
// else
// guessCount++
// console.log(generateHint(myGuess))
// Continue playing game
// end if
// else
// Tell user to make another selection
// end if

// generateHint(myGuess)
// // Test for exact Match
// Loop on the characters of myGuess
// if myGuess[i] === solution[i]
// exactMatch++
// end if
// end loop
//
// // Test for matching character. not in right position
// Loop on characters of myGuess
// if character exists in solution then
// matchChar++
// end if
// end loop
// return exactMatch + matchChar
*****************************************************************************************/

const assert = require('assert');
const readline = require('readline');
Expand All @@ -10,6 +58,7 @@ const rl = readline.createInterface({
let board = [];
let solution = '';
let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
const GUESSLIMIT = 20;

function printBoard() {
for (let i = 0; i < board.length; i++) {
Expand All @@ -28,21 +77,92 @@ function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}

function generateHint() {
// your code here
function generateHint(myGuess) {

// Initializing...
let exactMatch = 0;
let correctLetter = 0;
let matchIndex = -1;
// creating arrays out of the strings.
let solutionArr = solution.split('');
let myGuessArr = myGuess.split('');

// This piece of code tests for exact match
myGuessArr.forEach((letter, index) => {
if (letter === solutionArr[index]) {
exactMatch++;
// clear out value. This sets us up to look for correct letter test later. These won't be considered.
solutionArr[index] = '';
myGuessArr[index] = '';
}
});

// This piece of code tests for correct letter only
myGuessArr.forEach((letter) => {
if (letter) { // letter could be null from previous forEach statement.
matchIndex = solutionArr.indexOf(letter); // Let's find a matching letter in the solution!
if (matchIndex !== -1) { // We found a letter match in the string.
correctLetter++
solutionArr[matchIndex] = ''; // clear out value so value won't be considered with next letter tests.
}
} // closing brace for if (letter)
});

return `${exactMatch}-${correctLetter}`;
}

function mastermind(guess) {
solution = 'abcd'; // Comment this out to generate a random solution
// your code here
// validEntry() tests the user's entry. Valid entries only contain letters a-h. Only 4 letters.
function validEntry(myGuess) {
// Testing for length and testing if all the letters in guess are valid.
return myGuess.length === solution.length && myGuess.split('').every(myChar => letters.some(validLetter => myChar === validLetter));
}

// This i
function mastermind(guess) {
// solution = 'abca'; // Comment this out to generate a random solution
guess = guess.toLowerCase().trim(); // this is a "cleanup" statement will change all letters to lowercase and remove spaces.

if (validEntry(guess)) { // Testing to make sure
board[board.length] = `${guess} : ${generateHint(guess)}`;
if (guess === solution) {
return true; // player won!
}
} else {
console.log('Hey there, you need to re-enter your guess. Use letters a-h. Also, only 4 letters')
}
return false; // game is still going on.
}

function getPrompt() {
rl.question('guess: ', (guess) => {
mastermind(guess);
printBoard();
getPrompt();
rl.question('guess or (Q to quit): ', (guess) => {
// This statement gives the user some control to quit the game instead of CTRL-C.
if (guess.toLowerCase().trim() === 'q') {
console.log('Quiting...');
process.exit(0);
}

if (mastermind(guess)) {
printBoard();
console.log(`Great job. You won!\nYou cracked the code in ${board.length} moves.`);
console.log('Starting new game...');
// initialize variables...
board.length = 0; // clears board.
solution = ''; // set solution to NULL before generating new combination. Otherwise, it will append to the old.
generateSolution();
getPrompt();
} else if (board.length === GUESSLIMIT) {
printBoard();
console.log(`Sorry, you did not guess within ${GUESSLIMIT} moves. Answer: ${solution}.`);
console.log('Starting new game...');
// initialize variables...
board.length = 0; // clears board.
solution = ''; // set solution to NULL before generating new combination. Otherwise, it will append to the old.
generateSolution();
getPrompt();
} else {
printBoard();
getPrompt();
}
});
}

Expand Down