Guest User

Untitled

a guest
Apr 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. // ================================================================================
  2. // You will be writing a series of functions beneath each block of comments below.
  3. // Tackle each function one at a time. You may move on to a new exercise and return
  4. // later. Run the code and the console to the right will tell you if your function
  5. // is successfully passing the tests.
  6. // ================================================================================
  7.  
  8.  
  9. /*
  10. Define a function named `kmToMiles` that receives one parameter:
  11. 1) km - type: number
  12. The function should return a number in miles, rounded down to nearest integer.
  13.  
  14. HINT: There are 1.6km in 1 mile.
  15. HINT: What method on Javascript's Math object can round decimals down to nearest whole number?
  16. */
  17. function kmToMiles(km){
  18.  
  19. var miles = km/1.6;
  20.  
  21.  
  22. return Math.floor(miles);
  23. }
  24.  
  25. /*
  26. Define a function named `countLetters` that receives two parameters:
  27. 1) sentence - type: string
  28. 2) character - type: string
  29. The function should return a count of every `character` present in `sentence`.
  30.  
  31. HINT: You can access a single character in a string just like accessing an element in an array - `myString[3]` would access the fourth character
  32. */
  33.  
  34. function countLetters (sentence,character){
  35.  
  36. var counter=0;
  37.  
  38. for(var i=0;i < sentence.length; i++){
  39.  
  40.  
  41.  
  42. if(sentence[i] === character){
  43.  
  44. counter+=1;
  45. }
  46.  
  47.  
  48. }
  49.  
  50. return counter;
  51.  
  52.  
  53. }
  54.  
  55.  
  56. /*
  57. Define a function named `middleElement` that receives one parameter:
  58. 1) items - type: array
  59. The function should return the element present in the middle index. If
  60. the array contains an even number of elements, return the item in the
  61. first half of the array closest to the middle.
  62.  
  63. HINT: You can use the expression `n % 2 === 0` to check if `n` is an even number
  64. */
  65.  
  66. function middleElement(items){
  67.  
  68. if(items.length%2 === 0){
  69.  
  70. return items[(items.length*0.5)-1];
  71.  
  72. } else {
  73.  
  74.  
  75. return items[Math.round(items.length*0.5)-1];
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82. }
  83.  
  84.  
  85.  
  86. /*
  87. Define a function named `rightToVote` that receives one parameter:
  88. 1) person - type: object
  89. The function should check the `age` property on `person`. If it is 18 or older,
  90. return true. Otherwise, return false. If `age` is not a number, throw an error.
  91.  
  92. HINT: The `typeof` operator can tell you what data type a variable is
  93. */
  94.  
  95. function rightToVote(person){
  96.  
  97.  
  98. if (typeof person.age !== 'number'){
  99.  
  100. throw 'not a number!';
  101. }
  102.  
  103. if(person.age >= 18){
  104.  
  105. return true;
  106. } else {
  107.  
  108. return false;
  109. }
  110.  
  111.  
  112.  
  113. }
  114.  
  115.  
  116.  
  117.  
  118. /*
  119. Define a factory function `createStudent` that receives two parameters:
  120. 1) name - type: string
  121. 2) location - type: string
  122. This factory should return an object that includes the provided `name` and `location`
  123. properties and also has a `describe` method that returns the string: "Hello, I
  124. am {name} and I live in {location}."
  125.  
  126. HINT: You will want to use the `this` keyword
  127. */
  128.  
  129.  
  130.  
  131. function createStudent(name,location){
  132.  
  133.  
  134. var student = {
  135. name: name,
  136. location: location,
  137. describe: function(){
  138.  
  139. return `Hello, I am ${this.name} and I live in ${this.location}.`;
  140.  
  141.  
  142. }
  143.  
  144. }
  145.  
  146. return student;
  147.  
  148.  
  149. }
Add Comment
Please, Sign In to add comment