Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function main(array, letter) {
- var x = names(array);//console.log("x= ", x);
- var y = firstNames(x); //console.log("y= ", y);
- var z = countLetter(array,y,letter); //console.log("z= ", z);
- var p = names(z); //console.log("p= ", p);
- var q = firstNames(p);console.log("q= ", q); //console.log("mother before 1923= ",q);
- //console.log("People with two 'e' in first name are: ", q);
- var q1 = findMother(array); //console.log("Mother= ",q1);
- var q11 = names(q1);//console.log("q11= ", q11);
- var q2 = moreThanOneSon(q1); //console.log("q2= ",q2);
- var q21 = names(q2);//console.log("q21= ", q21);
- var q3 = oneSon(q11,q21); //console.log("q3= ",q3);
- var q4 = getSonData(q3,array); //console.log("q4= ", q4);
- var eAge = eAgeFunc(z,q4); console.log("Age difference= "+eAge);
- console.log("YESSSSSS! FINALLYYYYYYY !!!!" );
- }
- function eAgeFunc(aryAll, aryMom){
- var avg1=0, avgM=0, diff=0;
- avg1 = averageAge(aryAll);
- avgM = averageAge(aryMom);
- diff = avg1 - avgM;
- if (diff< 0){
- diff = avgM - avg1;
- }
- return diff;
- }
- function averageAge(array){
- var sumAge=0, avgAge=0;
- for (var i=0; i<array.length; i++){
- sumAge = sumAge + (array[i].died - array[i].born);
- }
- avgAge = sumAge/(i-1);
- return avgAge;
- }
- function getSonData(array1,stkarray){
- var temp=[], nameDone="";
- for (var i=0; i<array1.length; i++){
- for (var j=0; j<stkarray.length; j++){
- if (array1[i] == stkarray[j].name){
- temp[i] = stkarray[i];
- nameDone=stkarray[i];
- }
- }
- }console.log("temp= ",temp);
- return temp;
- }
- function oneSon(s1, s2){
- var temp=[], countS1=0,countS2=0, ck=0, t=[], a=0, count1=[], count2=[];
- var newS1=[];
- countS1 = counter(s1); console.log("count= "+count);
- countS2 = counter(s2); console.log("count= "+count);
- count2 = initialize(count1, countS1);
- for (var i=0; i<s1.length; i++){
- for (var j=0; j<s2.length; j++){
- if (s1[i] == s2[j])
- count2[i] ++;
- }
- }
- for (var i=0; i<s1.length; i++){
- if(count2[i] == 1){
- newS1.push(s1[i]);
- }
- }
- console.log("count2= "+count2);
- console.log("newS1= ", newS1);
- return newS1;
- }
- function initialize(array, count){
- for (var i=0; i<count; i++){
- array[i] = 0;
- }
- return array;
- }
- function counter(array, action){
- var sum=0;
- for (var i=0; i<array.length; i++){
- sum++;
- }
- return sum;
- }
- function moreThanOneSon(array){
- var temp =[];
- for (var i=0; i<array.length; i++){
- for (var j=0; j<array.length; j++){
- if(array[i] == array[j]){
- temp.push(array[i]);
- }
- }
- }
- return temp;
- }
- function findMother(array){
- var mother1923=[], mother=[];
- mother = forEach(array, function(x){return x.mother;});
- for (var i=0; i<mother.length; i++){
- for (var j=0; j<array.length; j++){
- if(mother[i] === array[j].name){
- if (array[i].born < 1923){
- mother1923.push(array[j]);
- }
- }
- }
- };
- return mother1923;
- }
- function forEach(array, action){
- var temp =[];
- for (var i=0; i<array.length; i++){
- temp[i] = action(array[i]);
- }
- return temp;
- }
- function names(array){
- var temp1 = [];
- temp1 = forEach(array, function(x){return x.name;});
- return temp1;
- }
- function firstNames(array){
- var temp2=[], temp3=[];
- temp2 = forEach(array, function(x){return x.split(" ");});
- console.log(temp2);
- for (var i=0; i<temp2.length; i++){
- temp3[i] = temp2[i][0];
- }
- return temp3;
- }
- function countLetter(stockarray, array,letter){
- var temp6=[], temp7=[];
- for(var i=0; i<array.length; i++){
- temp6[i] = count(array[i],letter);
- }
- for (var k=0; k<temp6.length; k++){
- if(temp6[k] == 2){
- temp7.push(stockarray[k]);
- };
- }
- return temp7;
- }
- function count(name,letter){
- var temp4=0, sum=0;
- for (var j=0; j<name.length; j++){
- if(name.charAt(j) == letter){sum++;};
- }
- return sum;
- }
- main(ancestry, "e");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement