diff --git a/01week/datatypes.js b/01week/datatypes.js index e69de29bb..8e4ef7556 100644 --- a/01week/datatypes.js +++ b/01week/datatypes.js @@ -0,0 +1,78 @@ +//1. Write a JavaScript program to display the current day and time. +function findTime(){ + const now = new Date(); //find date + const date = (now.getMonth() + 1) + "/" + now.getDate(); //display month/day + const time = now.getHours() + ":" + now.getMinutes(); //display hour:minute + const dayArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; //array for day of the week + const day = dayArray[now.getDay()]; //get string day of week + const dateTime = "It is " + day + ", " + date + " at " + time + "."; //display time and date in string + return dateTime; +} +findTime() + +//2. Write a JavaScript program to convert a number to a string. + +function numToString(num){ + return num.toString(); +} +numToString(30) + + +//3. Write a JavaScript program to convert a string to the number. + +function stringToNum(string){ + return Number(string); +} +stringToNum('89') + + +//4. Write a JavaScript program that takes in different datatypes and prints out what type they are. + +function dataType(type){ + return typeof(type); +} +dataType('eklgjheoh') + + +//5. Write a JavaScript program that adds 2 numbers together. + +function sumNumbers (num1,num2) { + return num1 + num2 +} +sumNumbers(7, 3000) + +//6. Write a JavaScript program that runs only when 2 things are true. + +function bothTrue (arg1, arg2) { + if(arg1 && arg2) { + return 'both are true' +}else{ + return 'nope' +} +} +bothTrue(4, 6); +bothTrue(null, 6); + +//7. Write a JavaScript program that runs when 1 of 2 things are true. + +function oneTrueThing(arg1, arg2) { + if (arg1 || arg2) { + return "one argument is true"; +} else { + return "Neither is true"; +} +} +oneTrueThing(true, false) +oneTrueThing(false, true) + +//8. Write a JavaScript program that runs when both things are not true. + +function bothFalse(arg1, arg2) { + if (!arg1 && !arg2) { + return "both are false" + } else { + return "at least one argument is true" + } +} +bothFalse(null, null) +bothFalse(0, 5) diff --git a/01week/rockPaperScissors.js b/01week/rockPaperScissors.js index 16f58790a..07c966625 100644 --- a/01week/rockPaperScissors.js +++ b/01week/rockPaperScissors.js @@ -7,13 +7,50 @@ const rl = readline.createInterface({ output: process.stdout }); +// an easier way to do this would have been if (hand1 === hand2) { +//return 'its a tie!'; +//} function rockPaperScissors(hand1, hand2) { - // Write code here + hand1=hand1.toLowerCase().trim(); + hand2=hand2.toLowerCase().trim(); + if (hand1 === 'rock'){ + if (hand2 === 'rock') { + return "It's a tie!"; + }else if (hand2 === 'paper'){ + return "Hand two wins!"; + }else if (hand2 === 'scissors') { + return "Hand one wins!"; + }else { + return "try again"; + } + }else if (hand1 === 'paper') { + if (hand2 === 'rock') { + return "Hand one wins!"; + }else if (hand2 === 'paper') { + return "It's a tie!"; + }else if (hand2 === 'scissors') { + return "Hand two wins!"; + }else { + return "try again"; + } + + }else if (hand1 === 'scissors') { + if (hand2 === 'rock') { + return "Hand two wins!"; + }else if (hand2 === 'paper'){ + return "Hand one wins!"; + } else if(hand2 === 'scissors') { + return "It's a tie!"; + }else { + return "try again"; + } + } } + function getPrompt() { rl.question('hand1: ', (answer1) => { rl.question('hand2: ', (answer2) => {