Advertisement
makispaiktis

Codecademy - 8th Exercise (Human Age as a Dog Age - Explain)

Sep 26th, 2019 (edited)
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. GETTING STARTED WITH JAVASCRIPT
  2. Dog Years
  3. Dogs mature at a faster rate than human beings. We often say a dog’s age can be calculated in “dog years” to account for their growth compared to a human of the same age. In some ways we could say, time moves quickly for dogs — 8 years in a human’s life equates to 45 years in a dog’s life. How old would you be if you were a dog?
  4.  
  5. Here’s how you convert your age from “human years” to “dog years”:
  6.  
  7. The first two years of a dog’s life count as 10.5 dog years each.
  8. Each year following equates to 4 dog years.
  9. Before you start doing the math in your head, let a computer take care of it! With your knowledge of math operators and variables, use JavaScript to convert your human age into dog years.
  10.  
  11. If you get stuck during this project or would like to see an experienced developer work through it, click “Get Help“ to see a project walkthrough video.
  12.  
  13. Tasks
  14. 9/10Complete
  15. Mark the tasks as complete by checking them off
  16. 1.
  17. Create a variable named myAge, and set it equal to your age as a number.
  18.  
  19. Write a comment that explains this line of code.
  20.  
  21. 2.
  22. Create a variable named earlyYears and save the value 2 to it. Note, the value saved to this variable will change.
  23.  
  24. Write a comment that explains this line of code.
  25.  
  26. 3.
  27. Use the multiplication assignment operator to multiply the value saved to earlyYears by 10.5 and reassign it to earlyYears.
  28.  
  29. 4.
  30. Since we already accounted for the first two years, take the myAge variable, and subtract 2 from it.
  31.  
  32. Set the result equal to a variable called laterYears. We’ll be changing this value later.
  33.  
  34. Write a comment that explains this line of code.
  35.  
  36. 5.
  37. Multiply the laterYears variable by 4 to calculate the number of dog years accounted for by your later years. Use the multiplication assignment operator to multiply and assign in one step.
  38.  
  39. Write a comment that explains this line of code.
  40.  
  41. 6.
  42. If you’d like to check your work at this point, print earlyYears and laterYears to the console. Are the values what you expected?
  43.  
  44. Check off this task when you’re ready to move on.
  45.  
  46. 7.
  47. Add earlyYears and laterYears together, and store that in a variable named myAgeInDogYears.
  48.  
  49. Write a comment that explains this line of code.
  50.  
  51. 8.
  52. Let’s use a string method next.
  53.  
  54. Write your name as a string, call its built-in method .toLowerCase(), and store the result in a variable called myName.
  55.  
  56. The toLowerCase method returns a string with all lowercase letters.
  57.  
  58. Write a comment that explains this line of code.
  59.  
  60. 9.
  61. Write a console.log statement that displays your name and age in dog years. Use string interpolation to display the value in the following sentence:
  62.  
  63. My name is NAME. I am HUMAN AGE years old in human years which is DOG AGE years old in dog years.
  64. Replace NAME with myName, HUMAN AGE with myAge, and DOG AGE with myAgeInDogYears in the sentence above.
  65.  
  66. Write a comment that explains this line of code.
  67.  
  68. 10.
  69. Great work! You can convert any human age to dog years. Try changing myAge and see what happens.
  70.  
  71. If you’d like extra practice, try writing this project without the *= operator.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement