Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 03week/readline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let readline = require('readline')

let cli = readline.createInterface({
input: process.stdin,
output: process.stdout
})

cli.question("What did you have for dinner?",(answer) => {

})
39 changes: 34 additions & 5 deletions 03week/towersOfHanoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,53 @@ function printStacks() {
console.log("c: " + stacks.c);
}

function movePiece() {
function isLegal(startStack, endStack) {
// Your code here

console.log(endStack)
if ((startStack === 'a' || startStack === 'b' || startStack === 'c') &&
(endStack === 'a' || endStack === 'b' || endStack === 'c')) {
let lastIndexStart = stacks[startStack].length - 1;
let lastIndexEnd = stacks[endStack].length - 1;
const moveTo = stacks[endStack];

const moveFrom = stacks[startStack];
if ((moveFrom[lastIndexStart] < moveTo[lastIndexEnd]) || (moveTo.length === 0)) {
console.log('legal')
return true;
} else {
console.log("Invalid entry");
return false;
}
} else {
console.log("Invalid entry");
}
}

function isLegal() {
function movePiece(startStack, endStack) {
// Your code here
let grabbed = stacks[startStack].pop();
console.log(grabbed)
stacks[endStack].push(grabbed);

}



function checkForWin() {
// Your code here

if (stacks.c.length === 4) {
console.log("You win!");
} else {
getPrompt();
}
}

function towersOfHanoi(startStack, endStack) {
// Your code here

if (isLegal(startStack, endStack) === true) {
movePiece(startStack, endStack);
checkForWin(startStack, endStack);
}
}

function getPrompt() {
Expand Down
52 changes: 48 additions & 4 deletions 04week/mastermind.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const rl = readline.createInterface({
let board = [];
let solution = '';
let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
let correctLetterLocation = 0;
let correctLetters = 0;

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

function generateHint() {
// your code here
function generateHint(guess, solution) {
const solutionArray = solution.split('');
const guessArray = guess.split('');
let targetIndex = null;
for(let i = 0; i < solutionArray.length; i++){
for(let j = 0; j < solutionArray.length; j++){
if((i === j) && (solutionArray[i] === guessArray[i]) && (solutionArray[i] !== null)){
solutionArray[i] = null;
correctLetterLocation++;
console.log('correctLettersLocation:', correctLetterLocation)
}else if (i !== j){
targetIndex = solutionArray.indexOf(guessArray[j]);
console.log('targetIndex:', targetIndex)
if(targetIndex > -1){
correctLetters++;
console.log('correctLetters:', correctLetters)
}
}
if(guessArray[i] === solutionArray[i]){
correctLetterLocation++
solutionArray[i] = null;
console.log('correctLetterLocation:', correctLetterLocation)
}else{
correctLetterLocation = 0;
}
}
return `${correctLetterLocation}-${correctLetters}`
}


function mastermind(guess) {
solution = 'abcd'; // Comment this out to generate a random solution
// your code here
solution = 'abcd';
if(guess === solution){
console.log("You guessed it!")
}else{
const hint = generateHint(guess, solution);
return hint;
}
}

// const acceptableGuess = (guess) => {
// if (guess.length === 4){
// let allLettersLegal = true;
// const guessArr = guess.split('');
// guessArr.forEach((letter) => {
// if(letters.indexOf(letter) === -1){
// allLettersLegal = false;
// }
// })
// }


function getPrompt() {
rl.question('guess: ', (guess) => {
Expand Down