From d5e21e1ec2450a7ead749b7aae1f88bbea0f953c Mon Sep 17 00:00:00 2001 From: Jim Schaefer Date: Thu, 10 Jan 2019 18:20:08 -0600 Subject: [PATCH 1/4] initial commit --- 01week/datatypes.js | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/01week/datatypes.js b/01week/datatypes.js index e69de29bb..890bcaf68 100644 --- a/01week/datatypes.js +++ b/01week/datatypes.js @@ -0,0 +1,60 @@ +// 1. Write a JavaScript program to display the current day and time. +const dateTime = new Date(); +document.write(dateTime); + +// 2. Write a JavaScript program to convert a number to a string. +var num = 10; +var str = num.toString(); +console.log(str) + +// 3. Write a JavaScript program to convert a string to the number. +var str = "10" +var num = Number(str) +console.log(typeof num) + +// 4. Write a JavaScript program that takes in different datatypes and prints out whether they are a: Boolean, Null, Undefined, Number, NaN, String +console.log(typeof true); +console.log(typeof null); +console.log(typeof x); +console.log(typeof 5); +console.log(typeof Nan) +console.log(typeof "Hello World") + +// 5. Write a JavaScript program that adds 2 numbers together. +var x = 5; +var y = 5; +var z = x + y; +console.log(z) + +// 6. Write a JavaScript program that runs only when 2 things are true. +function myFunction(val1, val2){ + if ((val1 % 2 == 1) && (val2 % 2 == 0)){ + return true + } else { + return false + } + } + console.log(myFunction(1, 2)) + +// 7. Write a JavaScript program that runs when 1 of 2 things are true. +function myFunction(val1, val2){ + if ((val1 % 2 == 0) || (val2 % 2 == 1)){ + return true + } else { + return false + } + } + console.log(myFunction(4, 2)) + +// 8. Write a JavaScript program that runs when both things are not true. +function myFunction(val1, val2){ + if ((val1 % 2 == 1) && (val2 % 2 == 1)){ + return false + } else { + return true + } + } + console.log(myFunction(4, 2)) + + + \ No newline at end of file From fe9f66d7a7a2cd2778766fea8651815e0db31627 Mon Sep 17 00:00:00 2001 From: Jim Schaefer Date: Tue, 15 Jan 2019 18:26:19 -0600 Subject: [PATCH 2/4] finished assignment --- 01week/rockPaperScissors.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/01week/rockPaperScissors.js b/01week/rockPaperScissors.js index 16f58790a..02d9b2ec4 100644 --- a/01week/rockPaperScissors.js +++ b/01week/rockPaperScissors.js @@ -10,7 +10,36 @@ const rl = readline.createInterface({ function rockPaperScissors(hand1, hand2) { - // Write code here +const player1 = hand1.toLowerCase(); +const player2 = hand2.toLowerCase(); + +if ( player1 === player2 ) { + return "It's a tie!"; +} + +if ( player1 === "rock" ){ + if (player2 === "scissors") { + return "Hand one wins!"; + } else if ( player2 === "paper" ) { + return "Hand two wins!"; + } +} + +if ( player1 === "paper" ){ + if (player2 === "scissors") { + return "Hand two wins!"; + } else if ( player2 === "rock" ) { + return "Hand one wins!"; + } +} + +if ( player1 === "scissors" ){ + if (player2 === "paper") { + return "Hand one wins!"; + } else if ( player2 === "rock" ) { + return "Hand two wins!"; + } +} } From 8a2c67c8486ab648c3d484aeff070e1b8bc42aed Mon Sep 17 00:00:00 2001 From: Jim Schaefer Date: Tue, 22 Jan 2019 21:06:04 -0600 Subject: [PATCH 3/4] updated week 1 homework files --- 02week/exercises.js | 17 ++++++++ 02week/pigLatin.js | 29 ++++++++++--- 02week/tests.js | 99 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 02week/exercises.js diff --git a/02week/exercises.js b/02week/exercises.js new file mode 100644 index 000000000..e82473db9 --- /dev/null +++ b/02week/exercises.js @@ -0,0 +1,17 @@ +const cars = ['ford', 'chevrolet', 'jeep', 'dodge'] +console.log(cars); + +const moreCars = ['kia','nissan','toyota','honda'] +console.log(moreCars); + +const totalCars = cars.concat(moreCars); +console.log(totalCars); + +console.log(moreCars.indexOf('honda')); + +console.log(cars.lastIndexOf('ford')); + +const stringOfCars = totalCars.join(); +console.log(stringOfCars); + + diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 046434c94..b5eef11db 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -1,4 +1,4 @@ -'use strict'; +'use wordict'; const assert = require('assert'); const readline = require('readline'); @@ -8,12 +8,32 @@ const rl = readline.createInterface({ }); -function pigLatin(word) { - // Your code here +function pigLatin(word){ + function checkVowels(element){ + let vowels = ['a', 'e', 'i','o','u'] + let passed = false; + for(let v of vowels){ + v === element ? passed = true : null; + } + return passed; + } -} + let vowelIndex = word.split('').findIndex(checkVowels) // returns the index of the first vowel + + let pigged = word; + + // if first letter is a vowel + vowelIndex === 0 ? pigged = word + 'way' : + // if the first two letters are consonants + vowelIndex > 1 || vowelIndex == -1 ? pigged = word.slice(2) + word.slice(0, 2) + 'ay' : + // if the first letter is a consonant and the second letter is a vowel + vowelIndex === 1 ? pigged = word.slice(1) + word[0] + 'ay' : null + + return pigged; + +} function getPrompt() { rl.question('word ', (answer) => { @@ -21,7 +41,6 @@ function getPrompt() { getPrompt(); }); } - // Tests if (typeof describe === 'function') { diff --git a/02week/tests.js b/02week/tests.js index e69de29bb..15ec10c3e 100644 --- a/02week/tests.js +++ b/02week/tests.js @@ -0,0 +1,99 @@ +'use strict'; + +const assert = require('assert'); +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + + +function rockPaperScissors(hand1, hand2) { + +const player1 = hand1.toLowerCase(); +const player2 = hand2.toLowerCase(); +const validAnswers = "rockpaperscissors"; + +if ((validAnswers.indexOf(player1) == 1) || (validAnswers.indexOf(player2) == -1)) { + return "Invalid answer(s)"; +} + +if ( player1 === player2 ) { + return "It's a tie!"; +} + +if ( player1 === "rock" ){ + if (player2 === "scissors") { + return "Hand one wins!"; + } else if ( player2 === "paper" ) { + return "Hand two wins!"; + } +} + +if ( player1 === "paper" ){ + if (player2 === "scissors") { + return "Hand two wins!"; + } else if ( player2 === "rock" ) { + return "Hand one wins!"; + } +} + +if ( player1 === "scissors" ){ + if (player2 === "paper") { + return "Hand one wins!"; + } else if ( player2 === "rock" ) { + return "Hand two wins!"; + } +} + +} + +function getPrompt() { + rl.question('hand1: ', (answer1) => { + rl.question('hand2: ', (answer2) => { + console.log( rockPaperScissors(answer1, answer2) ); + getPrompt(); + }); + }); +} + +// Tests + +if (typeof describe === 'function') { + + describe('#rockPaperScissors()', () => { + it('should detect a tie', () => { + assert.equal(rockPaperScissors('rock', 'rock'), "It's a tie!"); + assert.equal(rockPaperScissors('paper', 'paper'), "It's a tie!"); + assert.equal(rockPaperScissors('scissors', 'scissors'), "It's a tie!"); + }); + it('should detect which hand won', () => { + assert.equal(rockPaperScissors('rock', 'scissors'), "Hand one wins!"); + assert.equal(rockPaperScissors('rock', 'paper' ), "Hand two wins!"); + + assert.equal(rockPaperScissors('paper', 'rock' ), "Hand one wins!"); + assert.equal(rockPaperScissors('paper', 'scissors'), "Hand two wins!"); + + assert.equal(rockPaperScissors('scissors', 'paper' ), "Hand one wins!"); + assert.equal(rockPaperScissors('scissors', 'rock' ), "Hand two wins!"); + + }); + it('should scrub input to ensure lowercase with "trim"ed whitepace', () => { + assert.equal(rockPaperScissors('rOcK', ' paper '), "Hand two wins!"); + assert.equal(rockPaperScissors('Paper', 'SCISSORS'), "Hand two wins!"); + assert.equal(rockPaperScissors('rock ', 'sCiSsOrs'), "Hand one wins!"); + assert.equal(rockPaperScissors('RoCk', 'PaPeR' ), "Hand two wins!"); + assert.equal(rockPaperScissors('', '' ), "Hand _ wins!"); + }); + it('should check for valid entry'), () => { + assert.equal(rockPaperScissors('roq','payper'), "Invalid answer(s)" ) + assert.equal(rockPaperScissors('rawk','paeper'), "Invalid answer(s)" ) + assert.equal(rockPaperScissors('rick','peaper'), "Invalid answer(s)" ) + assert.equal(rockPaperScissors('rack','pappper'), "Invalid answer(s)" ) + } + }); +} else { + + getPrompt(); + +} From 133dc97c112c243708c36c8f994bec08a8edbc31 Mon Sep 17 00:00:00 2001 From: Jim Schaefer <43564443+jschaef80@users.noreply.github.com> Date: Tue, 22 Jan 2019 21:13:54 -0600 Subject: [PATCH 4/4] Delete exercises.js --- 02week/exercises.js | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 02week/exercises.js diff --git a/02week/exercises.js b/02week/exercises.js deleted file mode 100644 index e82473db9..000000000 --- a/02week/exercises.js +++ /dev/null @@ -1,17 +0,0 @@ -const cars = ['ford', 'chevrolet', 'jeep', 'dodge'] -console.log(cars); - -const moreCars = ['kia','nissan','toyota','honda'] -console.log(moreCars); - -const totalCars = cars.concat(moreCars); -console.log(totalCars); - -console.log(moreCars.indexOf('honda')); - -console.log(cars.lastIndexOf('ford')); - -const stringOfCars = totalCars.join(); -console.log(stringOfCars); - -