Guest User

Untitled

a guest
Jul 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. //Ex. 4.2
  2.  
  3. function range (num) {
  4. var arr = [];
  5. if (isNaN(Number(num)))
  6. alert ("Invalid number");
  7. else
  8. for (var i=0;i<num;i++)
  9. arr[i]=i;
  10. return arr;
  11. }
  12. range(5);
  13.  
  14. //Ex. 4.3
  15.  
  16. var mack = [];
  17. mack.push("Mack");
  18. mack.push("the");
  19. mack.push("Knife");
  20. show(mack.join(" "));
  21. show(mack.pop());
  22. show(mack);
  23. mack.push("bandit");
  24. mack.push("comes to town.");
  25.  
  26. /**
  27. js> mack
  28. Mack,the,Knife,bandit,comes to town.
  29. js> mack.join(" ")
  30. Mack the Knife bandit comes to town.
  31. js> mack.join(" ").split(" ")
  32. Mack,the,Knife,bandit,comes,to,town.
  33. */
  34.  
  35.  
  36. //join/split combo do not give back the original array if some of the values originally contained the join string.
  37.  
  38. function startsWith (first,second) {
  39. var chunk = first.slice(0,second.length);
  40. return chunk == second;
  41. }
  42.  
  43. /**
  44.  
  45. js> startsWith("sarah","sa")
  46. true
  47. js> startsWith("sarah", "sasasasa");
  48. false
  49. */
  50.  
  51. //Ex. 4.5
  52. function catNames (paragraph) {
  53. var lines = paragraph.split("/n");
  54. var catnames = [];
  55. for (var i=0;i<lines.length;i++) {
  56. var colon = lines[i].indexOf(":");
  57. if (colon >= 0) {// cat names here
  58. var names = lines[i].slice(colon+1);
  59. var cats = names.split(",");
  60. for (var j=0;j<cats.length;j++)
  61. catnames.push(cats[j]);
  62. }
  63. }
  64. return catnames;
  65. }
  66.  
  67. /**
  68. js> catNames(myparr);
  69. Doctor Hobbles the 2nd, Noog
  70. */
  71. // unfortunately, I get the space before "Doctor".
  72.  
  73. //Ex. 4.6
  74.  
  75. function extractDate (sentence) {
  76. var colon = sentence.indexOf(":");
  77. var beforeColon = sentence.slice(0,colon);
  78. var extract = beforeColon.split(" ");
  79. var thedate = extract[1];
  80. var compDate = thedate.split("/");
  81. return new Date(compDate[2],compDate[1],compDate[0]);
  82. }
  83.  
  84. var mystr = "died 27/04/2006: Black Leclère";
  85. extractDate(mystr);
  86.  
  87. //Ex 4.7
  88. function between (str, first, second) {
  89. var index1 = str.indexOf(first);
  90. var index2 = str.indexOf(second,index1);
  91. return str.slice(index1+2,index2);
  92.  
  93. }
  94.  
  95. /**
  96. js> var mystr = "bu ] boo [ bah ] gzz";
  97. js>
  98. js> between (mystr, "[ ", " ]");
  99. bah
  100. */
  101.  
  102.  
  103. // looking at the solution, I should have made my solution more general
  104.  
  105. // Ex. 4.8
  106.  
  107.  
  108. function formatDate(date) {
  109. var myday = date.getDate();
  110. if (myday < 10)
  111. myday = "0" + myday;
  112. var mymonth = date.getMonth() + 1;
  113. if (mymonth < 10)
  114. mymonth = "0" + mymonth;
  115. return myday + "/" + mymonth +
  116. "/" + date.getFullYear();
  117. }
  118.  
  119. // here again, looking at the solution, I feel short of the generalization required in the code
  120. // where a pad function within the function formatDate would have taken care of the code duplication.
  121. // I'll blame it on too much Java.
  122.  
  123. /**
  124. js> print (mydate);
  125. Wed Feb 09 2011 16:36:34 GMT-0500 (EST)
  126. js> formatDate (mydate);
  127. 09/02/2011
  128. */
  129.  
  130. // Ex. 4.9
  131. // oldest cat
  132. // Each cat object consists of property/value pair with birth: as a property
  133.  
  134.  
  135. function oldestCat (catData) {
  136. var oldest = "unknown";
  137. var birthdate = new Date().getTime();
  138. for (var cat in catData) {
  139. var catbirth = catData[cat].birth.getTime();
  140. if (catbirth < birthdate) {
  141. oldest = cat;
  142. birthdate = catbirth;
  143. }
  144. }
  145. return oldest;
  146. }
  147.  
  148. // okay, I didn't check to see if the cat was living or not !("death" in cat)
  149. // so I got "Spot" as the oldest cat instead of "Bugeye"
  150.  
  151. // Ex. 4.10
  152. // Extended range function to have a start-range number
  153.  
  154. function xRange (start, end) {
  155. if (arguments.length < 2) {
  156. end = start;
  157. start = 0;
  158. }
  159. if (start > end) {
  160. print (end + " not greater than " + start);
  161. return;
  162. }
  163.  
  164. var arr = [];
  165. var k = 0;
  166. for (var i=start;i<end;i++,k++)
  167. arr[k]=i;
  168. return arr;
  169. }
  170.  
  171. /**
  172. js> xRange(3,8);
  173. 3,4,5,6,7
  174. js> xRange(8);
  175. 0,1,2,3,4,5,6,7
  176. */
  177. // okay, looking at the solutions here, I should have used arr.push(i) to dynamically populate the array. No need for this extra k variable.
  178.  
  179. // Ex. 4.11
  180.  
  181. function sum (numList) {
  182. var sum = 0;
  183. for (var i=0;i<numList.length;i++)
  184. sum += numList[i];
  185. return sum;
  186. }
Add Comment
Please, Sign In to add comment