Advertisement
Guest User

Untitled

a guest
May 27th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. function main(array, letter) {
  2.  
  3. var x = names(array);//console.log("x= ", x);
  4. var y = firstNames(x); //console.log("y= ", y);
  5. var z = countLetter(array,y,letter); //console.log("z= ", z);
  6.  
  7. var p = names(z); //console.log("p= ", p);
  8. var q = firstNames(p);console.log("q= ", q); //console.log("mother before 1923= ",q);
  9. //console.log("People with two 'e' in first name are: ", q);
  10.  
  11. var q1 = findMother(array); //console.log("Mother= ",q1);
  12. var q11 = names(q1);//console.log("q11= ", q11);
  13. var q2 = moreThanOneSon(q1); //console.log("q2= ",q2);
  14. var q21 = names(q2);//console.log("q21= ", q21);
  15. var q3 = oneSon(q11,q21); //console.log("q3= ",q3);
  16. var q4 = getSonData(q3,array); //console.log("q4= ", q4);
  17.  
  18. var eAge = eAgeFunc(z,q4); console.log("Age difference= "+eAge);
  19. console.log("YESSSSSS! FINALLYYYYYYY !!!!" );
  20. }
  21.  
  22. function eAgeFunc(aryAll, aryMom){
  23. var avg1=0, avgM=0, diff=0;
  24. avg1 = averageAge(aryAll);
  25. avgM = averageAge(aryMom);
  26. diff = avg1 - avgM;
  27. if (diff< 0){
  28. diff = avgM - avg1;
  29. }
  30. return diff;
  31. }
  32.  
  33. function averageAge(array){
  34. var sumAge=0, avgAge=0;
  35. for (var i=0; i<array.length; i++){
  36. sumAge = sumAge + (array[i].died - array[i].born);
  37. }
  38. avgAge = sumAge/(i-1);
  39. return avgAge;
  40. }
  41.  
  42. function getSonData(array1,stkarray){
  43. var temp=[], nameDone="";
  44. for (var i=0; i<array1.length; i++){
  45. for (var j=0; j<stkarray.length; j++){
  46. if (array1[i] == stkarray[j].name){
  47. temp[i] = stkarray[i];
  48. nameDone=stkarray[i];
  49. }
  50. }
  51. }console.log("temp= ",temp);
  52. return temp;
  53. }
  54.  
  55. function oneSon(s1, s2){
  56. var temp=[], countS1=0,countS2=0, ck=0, t=[], a=0, count1=[], count2=[];
  57. var newS1=[];
  58. countS1 = counter(s1); console.log("count= "+count);
  59. countS2 = counter(s2); console.log("count= "+count);
  60. count2 = initialize(count1, countS1);
  61. for (var i=0; i<s1.length; i++){
  62. for (var j=0; j<s2.length; j++){
  63. if (s1[i] == s2[j])
  64. count2[i] ++;
  65. }
  66. }
  67. for (var i=0; i<s1.length; i++){
  68. if(count2[i] == 1){
  69. newS1.push(s1[i]);
  70. }
  71. }
  72. console.log("count2= "+count2);
  73. console.log("newS1= ", newS1);
  74. return newS1;
  75. }
  76.  
  77. function initialize(array, count){
  78. for (var i=0; i<count; i++){
  79. array[i] = 0;
  80. }
  81. return array;
  82. }
  83. function counter(array, action){
  84. var sum=0;
  85. for (var i=0; i<array.length; i++){
  86. sum++;
  87. }
  88. return sum;
  89. }
  90.  
  91. function moreThanOneSon(array){
  92. var temp =[];
  93. for (var i=0; i<array.length; i++){
  94. for (var j=0; j<array.length; j++){
  95. if(array[i] == array[j]){
  96. temp.push(array[i]);
  97. }
  98. }
  99. }
  100. return temp;
  101. }
  102.  
  103.  
  104. function findMother(array){
  105. var mother1923=[], mother=[];
  106. mother = forEach(array, function(x){return x.mother;});
  107. for (var i=0; i<mother.length; i++){
  108. for (var j=0; j<array.length; j++){
  109. if(mother[i] === array[j].name){
  110. if (array[i].born < 1923){
  111. mother1923.push(array[j]);
  112. }
  113. }
  114. }
  115. };
  116. return mother1923;
  117. }
  118.  
  119.  
  120. function forEach(array, action){
  121. var temp =[];
  122. for (var i=0; i<array.length; i++){
  123. temp[i] = action(array[i]);
  124. }
  125. return temp;
  126. }
  127.  
  128. function names(array){
  129. var temp1 = [];
  130. temp1 = forEach(array, function(x){return x.name;});
  131. return temp1;
  132. }
  133.  
  134. function firstNames(array){
  135. var temp2=[], temp3=[];
  136. temp2 = forEach(array, function(x){return x.split(" ");});
  137. console.log(temp2);
  138. for (var i=0; i<temp2.length; i++){
  139. temp3[i] = temp2[i][0];
  140. }
  141. return temp3;
  142. }
  143. function countLetter(stockarray, array,letter){
  144. var temp6=[], temp7=[];
  145. for(var i=0; i<array.length; i++){
  146. temp6[i] = count(array[i],letter);
  147.  
  148. }
  149. for (var k=0; k<temp6.length; k++){
  150. if(temp6[k] == 2){
  151. temp7.push(stockarray[k]);
  152. };
  153. }
  154. return temp7;
  155. }
  156. function count(name,letter){
  157. var temp4=0, sum=0;
  158. for (var j=0; j<name.length; j++){
  159. if(name.charAt(j) == letter){sum++;};
  160. }
  161. return sum;
  162. }
  163.  
  164. main(ancestry, "e");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement