0

I have made small algorithm and want to implement it using javascript. Here is my algorithm

I have a data in data.json file in this format

[
"109 200",
"109 201",
"102 202",
"103 202"
]

What I am trying to do is

  1. Create four array containers i.e. c1, c2, c3 and c4.
  2. Put above data in c1 container as it is
  3. loop through c1 and put the data in c4 in following format

    "109",
    "200",
    "109",
    "201",
    "102",
    "202",
    "103",
    "202"
    
  4. loop through c1 and put them in c2 in this format

    "109,200"
    
  5. then check if c3 is empty then read first value from c2 and push it in c3.

  6. repeat step 4 but this time put second data i.e. "109 201" from c1 in c2 in this format

    "109,201"
    
  7. then check if c3 is not empty then loop through c2 and check if any of these two values are repeated in c4. If it is repeated then repeat step 6 and 7 until it finds least amount of numbers from data.json.

This algorithm is not efficient but still I want to use this.

Here is my code.

var teams = [],
    c1 = [], arr = [], left = [], right = [], j = 0,
    show = function () {
        var span = $('#list');

        $.getJSON('data/data.json', function (ids) {

            //c1.push(c);
            for (var i = 0; i < ids.length; i++) {
                var a = smallcontainer(ids);
                var b = bigcontainer(ids);
                var c;

                if (c1 == "") {
                    c = a[0].split(" ");
                    console.log(c);
                } else {

                }


                //console.log(c);
                var id = ids[i];
                teams = id;
                $('<li>' + id + '</li>').appendTo(span);
            }
        });
    },

    smallcontainer = function (teams) { //arr
        arr = [teams[j]];
        j++;
        return arr;
    },

    bigcontainer = function (ids) { //c3. in code it is left+right=result
        for (var i = 0; i < ids.length; i++) {
            var splitted = ids[i].split(" ");
            left.push(splitted[0]);
            right.push(splitted[1]);

        }
        var result = left.concat(right);
    };

Update

data inside data.json file has four teams with two members in each team in this form

"109 200" = Team 1
"109 201" = Team 2
"102 202" = Team 3
"103 202" = Team 4

So now I have to compute the smallest number of people and it has to select one member from each team from this list and show their IDs. So the output for above would be

109
202

Latest update

I am still waiting for help

Solution

Here is the solution with the help of AlexBEll and PAEz. I used solution below which was basically solved by AlexBell

var data = [
 "1009 2000", 
 "1009 2001", 
 "1002 2002", 
 "1003 2002", 
 "1004 2003", 
 "1005 2004", 
 "1006 2005", 
 "1007 2006", 
 "1007 2007", 
 "1008 2008", 
 "1009 2008", 
 "1010 2009", 
 "1011 2010", 
 "1012 2010"      
];

var first = [], second = [], result = {}, out = '';

//Separe the ids
for(var i = 0; i < data.length; i++){
    var el = data[i].split(' '); 
    first[i] = el[0];
    second[i] = el[1];
}

for(var k = 0; k < first.length; k++){

    //Count the elements
    var nOfFirst = countElemnts(first, first[k]);
    var nOfSecond = countElemnts(second, second[k]);

    //If the first is in more that second take it
    if(nOfFirst > nOfSecond){
        result[first[k]] = 0;
    //Else take the second        
    }else if(nOfFirst < nOfSecond){
        result[second[k]] = 0;
    //If is only one take it    
    }else{
        result[first[k]] = 0;
    }

}

function countElemnts(arr, el){
    var count = 0;
    for(var j = 0; j < arr.length; j++){
        if(arr[j] == el)
            count++;
    }
    //console.log(el+' : '+count);
    return count;
}

for(var n in result){
    out += 'The id n: '+n+' is in the list\n';
}

alert(out);
35
  • No offense, but what is the point? The way I see it, in the end you're just replacing a space with a comma? Commented Aug 16, 2012 at 5:48
  • @Jeff good point. I am completely confused how to implement this so I will appreciate help. Commented Aug 16, 2012 at 5:49
  • Please post what the end result should be - that way we can compare the start result with the end result, and create a propper algorithm with the least amount of code. Commented Aug 16, 2012 at 5:51
  • ok I am updating my post Commented Aug 16, 2012 at 5:53
  • "smallest number of people from this list" - how did you get to that result then? I am clearly misunderstanding ;) Commented Aug 16, 2012 at 6:01

1 Answer 1

2

Does this work?....

var teams=[
"109 200",
"109 201",
"102 202",
"103 202"
];

var members ={};

var matesId='109';

// Members that won
var wins={};

// First lets find out how many teams a member is in
for (var i=0,length=teams.length; i<length;i++){
  var temp = teams[i].split(' ');
  for (var z=0,zlength=temp.length;z<zlength;z++){
    if (!members[temp[z]]) members[temp[z]]={wins:0,totalTeams:0,id:temp[z]};
      members[temp[z]].totalTeams=members[temp[z]].totalTeams+1;
  }
    teams[i]=[members[temp[0]],members[temp[1]]];
}

for (var i=0,length=teams.length; i<length;i++){
  var member1=teams[i][0];
  var member2=teams[i][1];
  if (member1.totalTeams>member2.totalTeams){
    member1.wins=member1.wins+1;
  } else if (member1.totalTeams<member2.totalTeams){
    member2.wins=member2.wins+1;
  } else {
    member1.wins=member1.wins+1;
    member2.wins=member2.wins+1;
  }    
}

for (var i=0,length=teams.length; i<length;i++){
  var member1=teams[i][0];
  var member2=teams[i][1];
  if (member1.wins>member2.wins){
    if (wins[member2.id]!==true) wins[member1.id]=true;
  } else if (member1.wins<member2.wins){
    if (wins[member1.id]!==true) wins[member2.id]=true;
  } else if (!wins[member1.id] && !wins[member2.id]) {
    if (member1.id==matesId && member2.id==matesId) {
      wins[matesId]=true;
    } else{
     // A draw, so pick one
      Math.round(Math.random())==1 ? wins[member2.id]=true : wins[member1.id]=true;
    }
  }    
}

var keys=Object.keys(wins);
var results=[];
results.push(keys.length);
for (var i=0,length=keys.length; i<length;i++){
  results.push(keys[i]);
}
results=results.join('\n');
document.querySelector('#output').innerText=results;​

http://jsfiddle.net/PAEz/dLUqj/3/
EDIT: Updated it so its a little easier to read.
EDIT: Realised you dont need a draw and win, just a win will do.
LAST EDIT: Noticed one small error, it should all be right now.

Sign up to request clarification or add additional context in comments.

4 Comments

What are you running it in? When I run it in Chrome the output shows 2 109 202
I got 3 warnings, just got rid of 2 but dont know how to get rid of the one on line 49..but it works. I wonder what your 4th warning is? Do you get any errors in chrome console?
no I am not getting errors in console. But it doesnt work at all for me.
Yep. jsfiddle.net/PAEz/dLUqj I was originally going to do it on JSFiddle, but thought Id give Bin a try. Works for me on the fiddle aswell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.