Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.86 KB | None | 0 0
  1. <!-- //// DAY 10 and 11 BOOLEANS AND COMPARISON OPERATORS. \\\\ -->
  2. <!-- LESSON 1 If Statements -->
  3. <!--1- You’re going to write a function that changes a check box depending on whether the answer given was correct or incorrect.
  4.  
  5. Write a function markAnswer(). Your function should expect two arguments:
  6.  
  7. - The first is a boolean value—if the answer was correct, this value will be TRUE otherwise it should be false.
  8. - The second argument will be an associative array representing the checkbox. Since this function will be making permanent changes to the box, it should be passed by reference to the function.
  9.  
  10. 2- Now to write the meat of the function!
  11.  
  12. If the answer was correct, the function should change the value at the box’s key of "shape" to "checkmark" and the value at the box’s key of "color" to "green". Otherwise, the function should change the value at the box’s key of "shape" to "x" and the value at the box’s key of "color" to "red". -->
  13. <?php
  14.  
  15. $learner_one = ["is_correct"=>FALSE, "box"=>["shape"=>"none", "color"=>"none"]];
  16.  
  17. $learner_two = ["is_correct"=>TRUE, "box"=>["shape"=>"none", "color"=>"none"]];
  18.  
  19.  
  20. function markAnswer($is_answer_correct, &$box){
  21. if ($is_answer_correct){
  22. $box["shape"] = "checkmark";
  23. $box["color"] = "green";
  24. } else {
  25. $box["shape"] = "x";
  26. $box["color"] = "red";
  27. }
  28. }
  29.  
  30. markAnswer($learner_one["is_correct"], $learner_one["box"]);
  31. markAnswer($learner_two["is_correct"], $learner_two["box"]);
  32.  
  33. print_r($learner_one["box"]);
  34. print_r($learner_two["box"]);
  35.  
  36. ?>
  37.  
  38. <!-- LESSON 2 Comparison Operators -->
  39. <!-- 1- Write a function chooseCheckoutLane(). This function should take in a single number argument representing the number of items a customer has. If the customer has 12 items or fewer, the function should return "express lane". Otherwise, the function should return "regular lane".
  40.  
  41. 2- In the U.S., citizens can vote if they are 18 years old or older. Write a function canIVote() that takes in a number representing an age, and return the string "yes" if they can vote, and the string "no" if they cannot.
  42.  
  43. 3- We tested your functions to make sure they work with various inputs, but you should be testing your own functions. Test each of your functions twice—once with an input that will enter the if block and once with an input that will enter the else block. Make sure to print the results to the terminal. -->
  44.  
  45. <?php
  46.  
  47. function chooseCheckoutLane($items)
  48. {
  49. if ($items <= 12){
  50. return "express lane";
  51. } else {
  52. return "regular lane";
  53. }
  54. }
  55.  
  56. function canIVote ($age)
  57. {
  58. if($age >= 18){
  59. return "yes";
  60. } else {
  61. return "no";
  62. }
  63. }
  64.  
  65. echo chooseCheckoutLane(11);
  66. echo chooseCheckoutLane(22);
  67. echo canIVote(21);
  68. echo canIVote(14);
  69.  
  70. ?>
  71.  
  72. <!-- LESSON 3 Identical and Not Identical Operators -->
  73. <!-- 1- Write a function agreeOrDisagree() that takes in two strings, and returns "You agree!" if the two strings are the same and "You disagree!" if the two strings are different.
  74.  
  75. 2- Test your function! Invoke it once with a value that will result in the if block running and once with a value that won’t.
  76.  
  77. 3- You’re going to write a function to check if it’s time for a user to renew their subscription.
  78.  
  79. Write a function checkRenewalMonth() that takes in a user’s renewal month, as a string (e.g. "January").
  80.  
  81. You function should get the current month using the PHP built-in date() function (see the hint for help with this). It should compare the current month to the renewal month passed in. If the renewal month is not the current month, the function should return the string "Welcome!". Otherwise it should return the string "Time to renew".
  82.  
  83. 4- Test your function! Invoke it once with a value that will result in the if block running and once with a value that won’t. -->
  84.  
  85. <?php
  86.  
  87. function agreeOrDisagree($name1, $name2)
  88. {
  89. if ($name1 === $name2){
  90. return "You agree!";
  91. } else {
  92. return "You disagree!";
  93. }
  94. }
  95. echo agreeOrDisagree("Jack", "Black");
  96. echo agreeOrDisagree("Jack", "Jack");
  97.  
  98. function checkRenewalMonth($renewal_month){
  99. $current_month = date("F");
  100. if ($renewal_month !== $current_month) {
  101. return "Welcome!";
  102. } else {
  103. return "Time to renew";
  104. }
  105. }
  106.  
  107. $current_month = date("F");
  108. echo checkRenewalMonth($current_month);
  109. echo checkRenewalMonth("July");
  110.  
  111. ?>
  112.  
  113. <!-- LESSON 4 Elseif Statements -->
  114. <!-- 1- You’re going to write a function to determine the likely genetic relationship between two people.
  115.  
  116. Write a function, whatRelation() that has one number parameter representing the percentage of DNA the two people share. Your function shoud print the likely relationship as a string. We expect the number passed in to always be an integer from 0 to 100
  117.  
  118. Here’s how it should calculate the relationship:
  119.  
  120. - 100 should print "identical twins"
  121. - 35 through99 should print "parent and child or full siblings"
  122. - 14through 34 should print "grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings"
  123. - 6 through 13 should print "first cousins"
  124. - 3 through 5 should print "second cousins"
  125. - 1 through 2 should print "third cousins"
  126. - 0 should print "not genetically related"
  127.  
  128. 2- Test your function with several inputs to make sure it’s working as expected. -->
  129.  
  130. <?php
  131.  
  132. function whatRelation ($percentSharedDNA)
  133. {
  134. if ($percentSharedDNA === 100){
  135. echo "identical twins";
  136. } elseif ($percentSharedDNA > 34){
  137. echo "parent and child or full siblings";
  138. } elseif ($percentSharedDNA > 13){
  139. echo "grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings";
  140. } elseif ($percentSharedDNA > 5) {
  141. echo "first cousins";
  142. } elseif ($percentSharedDNA > 2){
  143. echo "second cousins";
  144. } elseif ($percentSharedDNA > 0){
  145. echo "third cousins";
  146. } else {
  147. echo "not genetically related";
  148. }
  149. }
  150.  
  151. whatRelation(100);
  152. whatRelation(56);
  153. whatRelation(18);
  154. whatRelation(10);
  155. whatRelation(3);
  156. whatRelation(1);
  157.  
  158. ?>
  159.  
  160. <!-- LESSON 5 Switch Statement -->
  161. <!-- 1- The U.S. government has multiple classifications of air quality each symbolized by a color:
  162.  
  163. - The color "green" indicates the air quality is "good".
  164. - The color "yellow" indicates the air quality is "moderate".
  165. - The color "orange" indicates the air quality is "unhealthy for sensitive groups".
  166. - The color "red" indicates the air quality is "unhealthy".
  167. - The color "purple" indicates the air quality is "very unhealthy".
  168. . And the color "maroon" indicates the air quality is "hazardous".
  169.  
  170. Write a function, airQuality(), that takes in a color as a string, and prints the corresponding air quality.
  171.  
  172. Your function should use a switch statement. You should provide a default string of "invalid color" for any input aside from the six colors listed.
  173.  
  174. 2- Test your function with several inputs to make sure it’s working as expected. -->
  175.  
  176. <?php
  177.  
  178. function airQuality($color){
  179. switch ($color){
  180. case "green":
  181. echo "good";
  182. break;
  183. case "yellow":
  184. echo "moderate";
  185. break;
  186. case "orange":
  187. echo "unhealthy for sensitive groups";
  188. break;
  189. case "red":
  190. echo "unhealthy";
  191. break;
  192. case "purple":
  193. echo "very unhealthy";
  194. break;
  195. case "maroon":
  196. echo "hazardous";
  197. break;
  198. default:
  199. echo "invalid color";
  200. }
  201. }
  202.  
  203. airQuality("green");
  204. airQuality("yellow");
  205. airQuality("orange");
  206. airQuality("red");
  207. airQuality("wrong");
  208.  
  209. ?>
  210.  
  211. <!-- LESSON 6 Switch Statements: Fall through -->
  212. <!-- 1- Write a function, returnSeason(). Your function should take in a string representing a month (e.g. "January"), and it should return which season that month falls in.
  213.  
  214. - "December", "January", and "February" are "winter" months.
  215. - "March", "April", and "May" are "spring" months.
  216. - "June", "July", and "August" are "summer" months.
  217. - And "September", "October", and "November" are "fall" months.
  218.  
  219. Your function should use a switch statement and fall through to accomplish this task. -->
  220.  
  221. <?php
  222.  
  223. function returnSeason($season) {
  224. switch ($season) {
  225. case "December":
  226. case "January":
  227. case "February":
  228. return "winter";
  229. case "March":
  230. case "April":
  231. case "May":
  232. return "spring";
  233. case "June":
  234. case "July":
  235. case "August":
  236. return "summer";
  237. case "September":
  238. case "October":
  239. case "November":
  240. return "fall";
  241. }
  242. }
  243.  
  244. echo returnSeason("December");
  245. echo returnSeason("March");
  246. echo returnSeason("June");
  247. echo returnSeason("September");
  248.  
  249. ?>
  250.  
  251. <!-- LESSON 7 Ternary Operator -->
  252. <!-- 1- In a previous exercise, you wrote a function chooseCheckoutLane() using an if/else. Write a version of this function called ternaryCheckout() which accomplishes the same functionality using a ternary operator instead of an if/else.
  253.  
  254. This function should take in a single number argument representing the number of items a customer has. If the customer has 12 items or fewer, the function should return "express lane". Otherwise, the function should return "regular lane".
  255.  
  256. 2- In a previous exercise, you wrote a function canIVote() using an if/else. Write a version of this function called ternaryVote() which accomplishes the same functionality using a ternary operator instead of an if/else.
  257.  
  258. Your function should take in a number representing an age, and return the string “yes” if the age is greater than or equal to 18, and the string “no” it’s not.
  259.  
  260. 3- Test each of your functions twice—once with an input will fulfill the condition and once with an input that won’t. Make sure to print the results to the terminal. -->
  261.  
  262. <?php
  263.  
  264. function ternaryCheckout($items)
  265. {
  266. return $items <= 12 ? "express lane" : "regular lane";
  267. }
  268.  
  269. function ternaryVote($age)
  270. {
  271. return $age >= 18 ? "yes" : "no";
  272. }
  273.  
  274. echo ternaryCheckout(11);
  275. echo ternaryCheckout(16);
  276. echo ternaryVote(17);
  277. echo ternaryVote(21);
  278.  
  279. ?>
  280.  
  281. <!-- LESSON 8 Truthy and Falsy -->
  282. <!-- 1- Since it’s hard to keep track of what’s truthy or falsy in PHP, let’s write a function to check for us! Write a function truthyOrFalsy() that takes in any value and returns the string "True" if that value is truthy and the string "False" if that value is falsy.
  283.  
  284. 2- Test your function! Invoke your function at least once with a truthy value and at least once with a falsy value. Be sure to use echo to print the results to the terminal. -->
  285.  
  286. <?php
  287.  
  288. function truthyOrFalsy ($value)
  289. {
  290. if ($value){
  291. return "True";
  292. } else {
  293. return "False";
  294. }
  295. }
  296.  
  297. echo truthyOrFalsy(TRUE);
  298. echo truthyOrFalsy(FALSE);
  299. echo truthyOrFalsy("Arya");
  300. echo truthyOrFalsy("");
  301. echo truthyOrFalsy("13.12");
  302. echo truthyOrFalsy("0");
  303.  
  304. ?>
  305.  
  306. <!-- LESSON 9 User Input: readline() -->
  307. <!-- 1- In previous exercises that didn’t require user input, we ran your code for you. Now that you’re going to be using the terminal, you’ll also be running your own programs there. To run your program you’ll need to enter php src/index.php in the terminal.
  308.  
  309. We wrote the first line of a program in src/index.php. Run the program. You should see the text printed to the terminal.
  310.  
  311. 2- Time to expand the program in src/index.php. Use the readline() function to prompt a user.
  312.  
  313. You can use whatever you want as the prompt string. We tend to use ">> " or "> " because we feel it gives the user a clear idea of where to type. But test things out and see what you like!
  314.  
  315. The user will be entering their first name. You should save this response in a variable.
  316.  
  317. 3- Your program should handle three situations:
  318.  
  319. - If the user’s name is greater than 8 characters, you should print "Hi, [THEIR NAME]. That's a long name."
  320. - If their name is between 4 and 8 characters (inclusive), you should print "Hi, [THEIR NAME].".
  321. - And if their name is 3 characters or fewer, you should print "Hi, [THEIR NAME]. That's a short name.".
  322.  
  323. 4- Time to test your code. Run your code a couple times to make sure it’s working as expected. -->
  324.  
  325. <?php
  326.  
  327. echo "Hello, there. What's your first name?\n";
  328.  
  329. $name = readline(">> ");
  330.  
  331. $name_length = strlen($name);
  332.  
  333. if ($name_length > 8) {
  334. echo "Hi, ${name}. That's a long name.";
  335. } elseif ($name_length > 3) {
  336. echo "Hi, ${name}.";
  337. } else {
  338. echo "Hi, ${name}. That's a short name.";
  339. }
  340.  
  341. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement