From e6b4fc6597e753eb9eef0d2855e6130354464256 Mon Sep 17 00:00:00 2001 From: Matt Cameron Date: Sun, 21 Jan 2018 09:43:44 -0600 Subject: [PATCH 1/4] datatypes.js - added sumOfTwoNumbers --- 01week/datatypes.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/01week/datatypes.js b/01week/datatypes.js index e69de29bb..d89914767 100644 --- a/01week/datatypes.js +++ b/01week/datatypes.js @@ -0,0 +1,8 @@ +'use strict'; + +// Write a JavaScript program that adds 2 numbers together. +function sumOfTwoNumbers(num1, num2) { + return num1 + num2; +} + +console.log('2 + 2 should equal 4: ', sumOfTwoNumbers(2, 2) === 4); From 55ee2b380fca3a4ed983115d446d23738da60aa9 Mon Sep 17 00:00:00 2001 From: Matt Cameron Date: Sun, 21 Jan 2018 12:37:09 -0600 Subject: [PATCH 2/4] datatype.js - completed --- 01week/datatypes.js | 112 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 1 deletion(-) diff --git a/01week/datatypes.js b/01week/datatypes.js index d89914767..4f77b20d9 100644 --- a/01week/datatypes.js +++ b/01week/datatypes.js @@ -1,8 +1,118 @@ 'use strict'; -// Write a JavaScript program that adds 2 numbers together. +// 1. Write a JavaScript program to display the current day and time. +function displayDateAndTime() { + return Date(); +} + +console.log('\n\ndisplayDateAndTime'); +console.log('------------------'); +console.log('The current date and time is: ', displayDateAndTime()); + + + +// 2. Write a JavaScript program to convert a number to a string. +function convertNumberToString(num) { + // ensure num is really a number, to handle strings, nulls, etc + const convertedNum = Number(num); + return convertedNum.toString(); +} + +console.log('\n\nconvertNumberToString'); +console.log('---------------------'); +console.log("5 should convert to '5': ", convertNumberToString(5) === '5'); +console.log("5.5 should convert to '5.5': ", convertNumberToString(5.5) === '5.5'); +console.log("5.5 should convert to '5.5': ", convertNumberToString(5.5) === '5.5'); +console.log("'five' should convert to 'NaN': ", convertNumberToString() === 'NaN'); +console.log("null should convert to '0': ", convertNumberToString(null) === '0'); + + + +// 3. Write a JavaScript program to convert a string to the number. +function convertStringToNumber(str) { + return Number(str); +} + +console.log('\n\nconvertStringToNumber'); +console.log('---------------------'); +console.log("'5' should convert to 5: ", convertStringToNumber('5') === 5); +console.log("'5.5' should convert to 5.5: ", convertStringToNumber('5.5') === 5.5); +console.log("'five' should convert to NaN: ", Number.isNaN(convertStringToNumber('five'))); +console.log("'' should convert to 0: ", convertStringToNumber('') === 0); + + + +// 4. Write a JavaScript program that takes in different datatypes and prints out whether they are a: +// Boolean +// Null +// Undefined +// Number +// NaN +// String +function getDataType(val) { + // NaN and null are special cases + if (Number.isNaN(val)) return 'NaN'; + if (val === null) return 'null'; + + return typeof val; +} + +console.log('\n\ngetDataType'); +console.log('-----------'); +console.log("5 should return 'number': ", getDataType(5) === 'number'); +console.log("'5' should return 'string': ", getDataType('5') === 'string'); +console.log("true should return 'boolean': ", getDataType(true) === 'boolean'); +console.log("null should return 'null': ", getDataType(null) === 'null'); +console.log("undefined should return 'undefined': ", getDataType() === 'undefined'); +console.log("NaN should return 'NaN': ", getDataType(NaN) === 'NaN'); + + + +// 5. Write a JavaScript program that adds 2 numbers together. function sumOfTwoNumbers(num1, num2) { return num1 + num2; } +console.log('\n\nsumOfTwoNumbers'); +console.log('--------------'); console.log('2 + 2 should equal 4: ', sumOfTwoNumbers(2, 2) === 4); + + + +// 6. Write a JavaScript program that runs only when 2 things are true. +function numberIsBetween1And100(num) { + return num >= 1 && num <= 100; // inclusive +} + +console.log('\n\nnumberIsBetween1And100'); +console.log('----------------------'); +console.log('90 is between 1 and 100: ', numberIsBetween1And100(90) === true); +console.log('101 is NOT between 1 and 100: ', numberIsBetween1And100(101) !== true); + + + + +// 7. Write a JavaScript program that runs when 1 of 2 things are true. +function isZeroOrNegative(num) { + return num === 0 || num < 0; +} + +console.log('\n\nisZeroOrNegative'); +console.log('----------------'); +console.log('-1 is less than zero: ', isZeroOrNegative(-1) === true); +console.log('0 is zero: ', isZeroOrNegative(0) === true); +console.log('+1 is not zero or negative: ', isZeroOrNegative(1) !== true); + + + + +// 8. Write a JavaScript program that runs when both things are not true. +function isNotZeroOrNegative(num) { + return !(num === 0 || num < 0); +} + +console.log('\n\nisNotZeroOrNegative'); +console.log('-------------------'); +console.log('1 is NOT zero or negative: ', isNotZeroOrNegative(1) === true); +console.log('-1 IS zero or negative: ', isNotZeroOrNegative(-1) === false); +console.log('0 IS zero or negative: ', isNotZeroOrNegative(-1) === false); From fa92f33efea2cd2e632d46eabfaa997ebf5051f6 Mon Sep 17 00:00:00 2001 From: Matt Cameron Date: Sun, 21 Jan 2018 14:30:57 -0600 Subject: [PATCH 3/4] completed rockPaperScissors assignment --- 01week/rockPaperScissors.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/01week/rockPaperScissors.js b/01week/rockPaperScissors.js index 16f58790a..13b0f8925 100644 --- a/01week/rockPaperScissors.js +++ b/01week/rockPaperScissors.js @@ -7,11 +7,27 @@ const rl = readline.createInterface({ output: process.stdout }); +// a map that defines what beats what. ie, rock beats +// scissors, paper beats rock, etc. +const beats = { + "rock": "scissors", + "paper": "rock", + "scissors": "paper", +}; function rockPaperScissors(hand1, hand2) { - - // Write code here - + hand1 = hand1.toLowerCase().trim(); + hand2 = hand2.toLowerCase().trim(); + // check for tie + if (hand1 === hand2) { + return "It's a tie!"; + } + // look up what hand1 beats and see if it is hand2. + if (beats[hand1] === hand2) { + return "Hand one wins!" + } + // if not, hand 2 wins + return "Hand two wins!" } function getPrompt() { @@ -24,9 +40,7 @@ function getPrompt() { } // Tests - if (typeof describe === 'function') { - describe('#rockPaperScissors()', () => { it('should detect a tie', () => { assert.equal(rockPaperScissors('rock', 'rock'), "It's a tie!"); From 8e9c50a6b7a53e67fc3b87b8cecafcb314160ef3 Mon Sep 17 00:00:00 2001 From: Matt Cameron Date: Mon, 22 Jan 2018 22:08:48 -0600 Subject: [PATCH 4/4] updated problems 6, 7 and 8 --- 01week/datatypes.js | 48 ++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/01week/datatypes.js b/01week/datatypes.js index 4f77b20d9..4cce35f94 100644 --- a/01week/datatypes.js +++ b/01week/datatypes.js @@ -80,39 +80,39 @@ console.log('2 + 2 should equal 4: ', sumOfTwoNumbers(2, 2) === 4); // 6. Write a JavaScript program that runs only when 2 things are true. -function numberIsBetween1And100(num) { - return num >= 1 && num <= 100; // inclusive +function printSomethingWhenBothArgsAreTrue(arg1, arg2) { + if (arg1 && arg2) { + console.log('Both arguments are true!'); + } } -console.log('\n\nnumberIsBetween1And100'); -console.log('----------------------'); -console.log('90 is between 1 and 100: ', numberIsBetween1And100(90) === true); -console.log('101 is NOT between 1 and 100: ', numberIsBetween1And100(101) !== true); - - - +console.log('\n\printSomethingWhenBothArgsAreTrue'); +console.log('--------------------------------'); +printSomethingWhenBothArgsAreTrue(true, false); // should print nothing +printSomethingWhenBothArgsAreTrue(1, 2); // should print 'both args are true' // 7. Write a JavaScript program that runs when 1 of 2 things are true. -function isZeroOrNegative(num) { - return num === 0 || num < 0; +function printSomethingWhenOneArgIsTrue(arg1, arg2) { + if (arg1 || arg2) { + console.log('At least one argument is true!'); + } } -console.log('\n\nisZeroOrNegative'); -console.log('----------------'); -console.log('-1 is less than zero: ', isZeroOrNegative(-1) === true); -console.log('0 is zero: ', isZeroOrNegative(0) === true); -console.log('+1 is not zero or negative: ', isZeroOrNegative(1) !== true); - +console.log('\n\printSomethingWhenOneArgIsTrue'); +console.log('-----------------------------'); +printSomethingWhenOneArgIsTrue(true, false); // should print msg +printSomethingWhenOneArgIsTrue(0, null); // should print nothing // 8. Write a JavaScript program that runs when both things are not true. -function isNotZeroOrNegative(num) { - return !(num === 0 || num < 0); +function printSomethingWhenBothArgsAreFalse(arg1, arg2) { + if (!(arg1 || arg2)) { + console.log('Neither argument is true!'); + } } -console.log('\n\nisNotZeroOrNegative'); -console.log('-------------------'); -console.log('1 is NOT zero or negative: ', isNotZeroOrNegative(1) === true); -console.log('-1 IS zero or negative: ', isNotZeroOrNegative(-1) === false); -console.log('0 IS zero or negative: ', isNotZeroOrNegative(-1) === false); +console.log('\n\printSomethingWhenBothArgsAreFalse'); +console.log('---------------------------------'); +printSomethingWhenBothArgsAreFalse(true, 0); // should not print msg +printSomethingWhenBothArgsAreFalse(null, undefined); // should print msg \ No newline at end of file