From bd21365883d61ffd1271bc07125b9a063105bcf2 Mon Sep 17 00:00:00 2001 From: GoldenPedro Date: Tue, 11 Aug 2020 17:49:44 -0500 Subject: [PATCH] solved index.js tasks --- index.js | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 73f240372..183b8d95f 100644 --- a/index.js +++ b/index.js @@ -2,24 +2,44 @@ //Task a: declare a variable called votingAge, console log true if age > 18 (no function required) +let votingAge = 19; + +if (votingAge > 18) { + console.log("true"); +} + //Task b: declare a variable and then use a conditional to change the value of that variable based on the value assigned to a second variable (no function required) +let drink = "coffee"; +let night = true +if (night = false) { + console.log(drink); +} else if (night = true) { + let drink = "tea"; + console.log(drink); +} //Task c: Convert string ("1999") to integer (1999) (no function required) // hint look up the Number method - +var numbers = "1999" +numbers.toString(); +console.log(numbers); //Task d: Write a function to multiply a*b +function multiplication(a, b) { + return a * b; +} +console.log(multiplication(3, 5)); @@ -27,7 +47,10 @@ //Age in Dog years //write a function that takes your age and returns it to you in dog years - they say that 1 human year is equal to seven dog years - +function dogYears(a) { + return a * 7; +} + console.log(dogYears(22)); @@ -49,7 +72,29 @@ // when you are finished invoke your function with the weight of 15 lbs and the age of 1 year - if your calculations are correct your result should be 0.44999999999999996 +function dogFeeder(age, weight) { + var poundsOfFood = 0; + + if (age >= 1 && weight <= 5) { + var poundsOfFood = weight * 0.05; + } else if (age >= 1 && weight >= 6 && weight <= 10) { + var poundsOfFood = weight * 0.04; + } else if (age >= 1 && weight >= 11 && weight <= 15) { + var poundsOfFood = weight * 0.03; + } else if (age >= 1 && weight >= 15) { + var poundsOfFood = weight * 0.02; + } else if (age >= .16 && age <= .33) { + var poundsOfFood = weight * 0.1; + } else if (age >= .33 && age <= .583) { + var poundsOfFood = weight * 0.05; + } else if (age >= .583 && age <= .99) { + var poundsOfFood = weight * 0.04; + } + console.log(poundsOfFood); +} + +dogFeeder(1, 15); @@ -60,20 +105,45 @@ // use math.random to determine the computers choice // hint while you can complete this with only conditionals based on strings it may help to equate choice to a number - +function computerPlay() { + var myArray = ["Rock", "Paper", "Scissors"]; + return myArray[Math.floor(Math.random() * myArray.length)]; +} + +function game(playerChoice) { + var computerChoice = computerPlay(); + if (playerChoice == "Rock" && computerChoice == "Scissors") { + console.log("You won!"); +} else if (playerChoice == "Paper" && computerChoice == "Rock") { + console.log("You won!"); +} else if (playerChoice == "Scissors" && computerChoice == "Paper") { + console.log("You won!"); +} else if (playerChoice == computerChoice) { + console.log("Draw!"); +} else { + console.log("You lost!"); +} +} +game("Paper"); /************************************************************** Task 5 **************************************************************/ //Metric Converter //a. KM to Miles - should take the number of kilometers and convert it to the equal number of miles - +function kilometersToMiles(a) { + return a * 0.6214 +} +console.log(kilometersToMiles(5)) //b. Feet to CM - should take the number of feet and convert it to the equal number of centimeters - +function feetToCentimeters(a) { + return a / 0.032808 +} +console.log(feetToCentimeters(5)) @@ -82,7 +152,13 @@ // create a function called annoyingSong // the function should take a starting number as an argument and count down - at each iteration it should log (number) bottles of soda on the wall, (number) bottles of soda, take one down pass it around (number left over) bottles of soda on the wall` +function annoyingSong(a) { + for (a; a > 0; a--) { + console.log(a + " bottles of soda on the wall, " + a + " bottles of soda, take one down pass it around " + (a-1) + " bottles of soda on the wall") + } +} +annoyingSong(10); @@ -95,7 +171,23 @@ //60s should be D //and anything below 60 should be F - +function gradeCalculator(a) { + a = a / 100; + console.log(a); + if (a >= .9) { + console.log("You got an A"); + } else if (a >= .8 && a <= .89) { + console.log("You got an B"); + } else if (a >= .7 && a <= .79) { + console.log("You got an C"); + } else if (a >= .6 && a <= .69) { + console.log("You got an D"); + } else if (a < .6) { + console.log("You got an F"); + } +} + +gradeCalculator(67);