Advertisement
makispaiktis

Codecademy - 12th Exercise (14 different little projects)

Oct 3rd, 2019 (edited)
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1.
  2.  
  3. function canIVote(age){
  4.   if(age < 18){
  5.     console.log("You cannot vote now.");
  6.     return false;
  7.   }
  8.   else{
  9.     console.log("You can vote, you are up to 18 years old.");
  10.     return true;
  11.   }
  12. }
  13.  
  14. console.log(canIVote(19)) // Should print true
  15.  
  16.  
  17.  
  18.  
  19. 2.
  20.  
  21. function agreeOrDisagree(string1, string2){
  22.     return string1 === string2 ? "You agree!" : "You disagree!";
  23. }
  24.  
  25. console.log(agreeOrDisagree("yep", "yep")) // Prints "You agree!"
  26.  
  27.  
  28.  
  29.  
  30. 3.
  31.  
  32. const lifePhase = (age) => {
  33.   if(age < 0 || age > 140){
  34.     return "This is not a valid age";
  35.   }
  36.   else if(age > 0 && age <= 3){
  37.     return "baby";
  38.   }
  39.   else if(age >= 4 && age <= 12){
  40.     return "child";
  41.   }
  42.   else if(age >= 13 && age <= 19){
  43.     return "teen";
  44.   }
  45.   else if(age >= 20 && age <= 64){
  46.     return "adult";
  47.   }
  48.   else if(age >= 65 && age <= 140){
  49.     return "senior citizen";
  50.   }
  51. }
  52.  
  53. console.log(lifePhase(3.5)) //should print 'child'
  54.  
  55.  
  56.  
  57.  
  58. 4.
  59.  
  60. function finalGrade(num1, num2, num3){
  61.   if(num1 < 0 || num1 > 100 || num2 < 0 || num2 > 100 || num3 < 0 || num3 > 100){
  62.     return "You have entered an invalid grade."  
  63.   }
  64.   const average = (num1 + num2 + num3) / 3;
  65.   console.log("Average: " + average);
  66.   if(average >= 0 && average <= 59){
  67.     return "F";
  68.   }
  69.   if(average >= 60 && average <= 69){
  70.     return "D";
  71.   }
  72.   if(average >= 70 && average <= 79){
  73.     return "C";
  74.   }
  75.   if(average >= 80 && average <= 89){
  76.     return "B";
  77.   }
  78.   if(average >= 90 && average <= 100){
  79.     return "A";
  80.   }
  81. }
  82.  
  83. console.log(finalGrade(99, 92, 95)) // Should print 'A'
  84.  
  85.  
  86.  
  87.  
  88. 5.
  89.  
  90. const reportingForDuty = (rank, lastName) => (rank + " " + lastName + " reporting for duty!");
  91.  
  92. console.log(reportingForDuty('Private', 'Fido')) // Should return 'Private Fido reporting for duty!'
  93.  
  94.  
  95.  
  96. 6.
  97.  
  98. const rollTheDice = () => {
  99.   // Math.random() gives us a random number from 0 up to, but not including, 1
  100.   // We multiplied that by 6 to get a number between 0 and up to, but not including, 6
  101.   // But since we actually wanted numbers from 1 to 6, inclusive, we added 1
  102.     let die1 = Math.floor(Math.random() * 6 + 1);
  103.     let die2 = Math.floor(Math.random() * 6 + 1);
  104.     return die1 + die2
  105. }
  106.  
  107.  
  108.  
  109. 7.
  110.  
  111. function calculateWeight(earthWeight, planet){
  112.   switch(planet){
  113.     case "Mercury":
  114.       return 0.378 * earthWeight;
  115.       break;        //  Not necessary, because of return-statement above
  116.     case "Venus":
  117.       return 0.907 * earthWeight;
  118.       break;        //  Not necessary, because of return-statement above
  119.         case "Mars":
  120.       return 0.377 * earthWeight;
  121.       break;        //  Not necessary, because of return-statement above
  122.     case "Jupiter":
  123.       return 2.36 * earthWeight;
  124.       break;        //  Not necessary, because of return-statement above
  125.     case "Saturn":
  126.       return 0.916 * earthWeight;break;     //  Not necessary, because of return-statement above
  127.     default:
  128.             return "Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.";
  129.     }
  130. }
  131.  
  132. console.log(calculateWeight(100, 'Jupiter')) // Should print 236
  133.  
  134.  
  135.  
  136.  
  137. 8.
  138.  
  139. const truthyOrFalsy1111 = value => {
  140.     if (value) {
  141.         return true
  142.     }
  143.     return false
  144. }
  145.  
  146. // Write your function here:
  147. function truthyOrFalsy(value){
  148.   if(value === false || value === 0  || value === "" || value === '' || value === `` || value === null || value === undefined || value === NaN){
  149.     return false;
  150.   }
  151.   else{
  152.     return true;
  153.   }
  154. }
  155.  
  156. console.log(truthyOrFalsy(0)) // Should print false
  157.  
  158.  
  159.  
  160.  
  161. 9.
  162.  
  163. // Write your function here:
  164. const numImaginaryFriends = (friends) => {
  165.   return Math.round(0.33 * friends);
  166. }
  167.  
  168. // Uncomment the line below when you're ready to try out your function
  169. console.log(numImaginaryFriends(18)) // Should print 6
  170.  
  171.  
  172.  
  173. 10.
  174.  
  175. const sillySentence = (adjective, verb, noun) => `I am so ${adjective} because I ${verb} coding! Time to write some more awesome ${noun}!`;
  176.  
  177. console.log(sillySentence('excited', 'love', 'functions'); );
  178.  
  179.  
  180.  
  181. 11.
  182.  
  183. // Write your function here:
  184. function howOld(age, year){
  185.   if(year >= 2020){
  186.     return `You will be ${age + year - 2019} in the year ${year}.`
  187.   }
  188.   else if(2019 - age > year){
  189.     return `The year ${year} was ${2019 - age - year} years before you were born.`;
  190.   }
  191.   else if(2019 - age < year && year <= 2019){
  192.     return `You were ${year - 2019 + age} in the year ${year}.`;
  193.   }
  194. }
  195.  
  196. console.log("This code was written in 2019 and I am born at 1999, so suppose that current year is 2019 and I was 20 years old.");
  197. console.log();
  198. console.log("1. " + howOld(20, 2039));
  199. console.log("2. " + howOld(20, 2009));
  200. console.log("3. " + howOld(20, 1909));
  201.  
  202.  
  203.  
  204. 12.
  205.  
  206. const whatRelation = percentSharedDNA => {
  207.     if (percentSharedDNA === 100) {
  208.         return 'You are likely identical twins.'
  209.     }
  210.     else if (percentSharedDNA <= 99 && percentSharedDNA > 34) {
  211.         return 'You are likely parent and child or full siblings.'
  212.     }
  213.     else if (percentSharedDNA <= 34 && percentSharedDNA > 13) {
  214.         return 'You are likely grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings.'
  215.     }
  216.     else if (percentSharedDNA > 5 && percentSharedDNA <=13) {
  217.         return 'You are likely 1st cousins.'
  218.     }
  219.     else if (percentSharedDNA > 2 && percentSharedDNA <= 5) {
  220.         return 'You are likely 2nd cousins.'
  221.     }
  222.     else if (percentSharedDNA > 0 && percentSharedDNA <= 2) {
  223.         return 'You are likely 3rd cousins'
  224.     }
  225.     return 'You are likely not related.'
  226. }
  227.  
  228. console.log(whatRelation(34))
  229. // Should print 'You are likely grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings.'
  230.  
  231. console.log(whatRelation(3))
  232. // Should print 'You are likely 2nd cousins.'
  233.  
  234.  
  235.  
  236.  
  237. 13.
  238.  
  239. // Write your function here:
  240. const tipCalculator = (quality, total) => {
  241.   switch(quality){
  242.     case "bad":
  243.       return 0.05 * total;
  244.       break;
  245.     case "ok":
  246.         return 0.15 * total;
  247.       break;
  248.     case "good":
  249.         return 0.2 * total;
  250.       break;
  251.     case "excellent":
  252.       return 0.3 * total;
  253.       break;
  254.     default:
  255.       return 0.18 * total;
  256.       break;
  257.   }
  258. }
  259.  
  260. console.log(tipCalculator('good', 100)) //should return 20
  261.  
  262.  
  263.  
  264. 14.
  265.  
  266. // Write your function here:
  267. function toEmoticon(emotion){
  268.   switch(emotion){
  269.     case "shrug":
  270.       return '|_{"}_|';
  271.         break;
  272.     case "smiley face":
  273.       return ':)';
  274.             break;
  275.         case "frowny face":
  276.       return ':(';
  277.             break;
  278.         case "winky face":
  279.       return ';)';
  280.             break;
  281.         case "heart":
  282.       return '<3';
  283.             break;
  284.         default:
  285.             return "|_(* ~ *)_|";
  286.             break;
  287.   }
  288. }
  289.  
  290. // console.log(toEmoticon("whatever"))
  291. // Should print  '|_(* ~ *)_|'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement