Adding up values from random arrray

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 852 Views - Last Post: 06 November 2013 - 08:55 PM

#1 EdNolan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 241
  • Joined: 22-September 13

Adding up values from random arrray

Posted 29 October 2013 - 08:27 PM

Can anyone tell me why this doesn't work.


var shownImages = ["33.png","1.png", "15.png", "10.png", "18.png", "12.png", "9.png", "8.png", "5.png", 
"11.png", "7.png", "13.png", "14.png", "4.png", "17.png", "6.png", "19.png", 
"20.png",  "22.png", "30.png", "2.png", "32.png"];

shownImages.sort();
var groupOne = 0;
var one = 0;
var two = 0;
var three = 0;
var four = 0;
for(i = 0; i < shownImages.length; i++){
    if(shownImages[i] == 1.png)
       { one = 1; }
    if(shownImages[i] == 2.png)
       { two = 1; }
    if(shownImages[i] == 3.png)
       { three = 1; }
    if(shownImages[i] == 4.png)
       { four = 1; }
}
groupOne = one + two + three + four




Is This A Good Question/Topic? 0
  • +

Replies To: Adding up values from random arrray

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: Adding up values from random arrray

Posted 29 October 2013 - 11:25 PM

lines #12, #14, #16, #18: .png is not a property of Number (hence returning undefined) and therfore there is no match in your conditions.
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: Adding up values from random arrray

Posted 29 October 2013 - 11:31 PM

tip: for (i = 0, l = shownImages.length; i < l; i++) saves you the hassle of calculating the array length in each loop cycle. alternate syntax (if it doesnít matter whether the indexes go up or down): for (i = shownImages.length; i--; )
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: Adding up values from random arrray

Posted 29 October 2013 - 11:49 PM

just an alternate approach:
function intersect(arr1, arr2) {
  return arr1.filter(function(item) {
    return (arr2.indexOf(item) !== -1);
  });
}

var groupOne = intersect(shownImages, ["1.png", "2.png", "3.png", "4.png"]).length;


Iím not sure about the intend of your code, but this does the same.
Was This Post Helpful? 0
  • +
  • -

#5 EdNolan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 241
  • Joined: 22-September 13

Re: Adding up values from random arrray

Posted 30 October 2013 - 05:29 PM

View PostDormilich, on 29 October 2013 - 11:31 PM, said:

tip: for (i = 0, l = shownImages.length; i < l; i++) saves you the hassle of calculating the array length in each loop cycle. alternate syntax (if it doesnít matter whether the indexes go up or down): for (i = shownImages.length; i--; )

Thank you for all the details. The more the better since it really help me learn and understand. I believe I do need to calculate the length. This code is being placed inside a loop and new variables will be added to the array each loop. I think the alternate approach you offer is what I really need. The reason I say that is because I am mostly interested in the value, and not so much the index. I am checking to see what values are in the array, and then I want to group the values like this, 1-4,5-8,9-12,13-16,17-20...49-52. Once I know what values are in the array I want to return how many values where found for each group. So in my first post, the array had values 1.png, 2.png and 4.png so group one = 3. Group two also equals 3 because the array had values 5.png, 6.png and 8.png. etc.

Umm - I will look at this more but one question about the alternate approach, the (function(item)) can I do something like...
 <table>
            <tr><td><div id="group1"></div></td><td><div id="group2"></div></td><td><div id="group3"></div></td></tr>
        </table>
                function doMe(shownImages) {
                document.getElementById("group1").innerHTML = shownImages
                     }
// I want to show each group total with something like <div>. Am I any closer now that I edited your code to reflect all the groups?

function intersect(shownImages) {
  return shownImages.filter(function(item);
}

var groupOne = intersect(shownImages, ["1.png", "2.png", "3.png", "4.png"]).length;
var groupTwo = intersect(shownImages, ["5.png", "6.png", "7.png", "8.png"]).length;
var groupThree = intersect(shownImages, ["9.png", "10.png", "11.png", "12.png"]).length;
var groupFour = intersect(shownImages, ["13.png", "14.png", "15.png", "16.png"]).length;
var groupFive = intersect(shownImages, ["17.png", "18.png", "19.png", "20.png"]).length;
var groupSix = intersect(shownImages, ["21.png", "22.png", "23.png", "24.png"]).length;
var groupSeven = intersect(shownImages, ["25.png", "26.png", "27.png", "28.png"]).length;
var groupEight = intersect(shownImages, ["29.png", "30.png", "31.png", "32.png"]).length;
var groupNine = intersect(shownImages, ["33.png", "34.png", "35.png", "36.png"]).length;
var groupTen = intersect(shownImages, ["37.png", "38.png", "39.png", "40.png"]).length;
var groupEleven = intersect(shownImages, ["41.png", "42.png", "43.png", "44.png"]).length;
var groupTwelve = intersect(shownImages, ["45.png", "46.png", "47.png", "48.png"]).length;
var groupThirteen = intersect(shownImages, ["49.png", "50.png", "51.png", "52.png"]).length;


Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: Adding up values from random arrray

Posted 30 October 2013 - 05:37 PM

line #10 seems truncated. that code should throw a syntax error.
Was This Post Helpful? 0
  • +
  • -

#7 EdNolan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 241
  • Joined: 22-September 13

Re: Adding up values from random arrray

Posted 30 October 2013 - 06:26 PM

sorry, I actually corrected that but forgot to put that correction in the post. Bed time. I'll research more tmrw and see what I cancome up with.
  return shownImages.filter(function(item));


Was This Post Helpful? 0
  • +
  • -

#8 EdNolan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 241
  • Joined: 22-September 13

Re: Adding up values from random arrray

Posted 30 October 2013 - 07:54 PM

Dormilich, you rock !!! I'm going to use these. I understand arrays better now. If I am correct i = 0 is usually used for the index, and to get the value/object/variable I have to use a var of some sort. I was so torn up on how to seperate the two but I understand better now! You rock!
var groupOne = intersect(shownImages, ["1.png", "2.png", "3.png", "4.png"]).length;
var groupTwo = intersect(shownImages, ["5.png", "6.png", "7.png", "8.png"]).length;
var groupThree = intersect(shownImages, ["9.png", "10.png", "11.png", "12.png"]).length;
var groupFour = intersect(shownImages, ["13.png", "14.png", "15.png", "16.png"]).length;
var groupFive = intersect(shownImages, ["17.png", "18.png", "19.png", "20.png"]).length;
var groupSix = intersect(shownImages, ["21.png", "22.png", "23.png", "24.png"]).length;
var groupSeven = intersect(shownImages, ["25.png", "26.png", "27.png", "28.png"]).length;
var groupEight = intersect(shownImages, ["29.png", "30.png", "31.png", "32.png"]).length;
var groupNine = intersect(shownImages, ["33.png", "34.png", "35.png", "36.png"]).length;
var groupTen = intersect(shownImages, ["37.png", "38.png", "39.png", "40.png"]).length;
var groupEleven = intersect(shownImages, ["41.png", "42.png", "43.png", "44.png"]).length;
var groupTwelve = intersect(shownImages, ["45.png", "46.png", "47.png", "48.png"]).length;
var groupThirteen = intersect(shownImages, ["49.png", "50.png", "51.png", "52.png"]).length;



Now I have to work on comparing the groups var's above to the odds array below.

This code below does not work when I test it. Any Idea's, hints? I want to display the odds in div.


<table>
      <tr>
      <td><div id="group1"></div></td><td><div id="group2"></div></td><td><div id="group3"></div></td>
      </tr>
</table>

var oddsAftNinthPick = ["10:1", "14:1", "21:1", "43:1", "0:0"];
function compare() { 
             if (groupOne == 0) {                //10:1
                var odds = oddsAftNinthPick[0];
                document.getElementById("group1").innerHTML;
                                  }
}
             if (groupOne == 1) {                //14:1
                var odds = oddsAftNinthPick[1];
                document.getElementById("group1").innerHTML;
                                  }
}
             if (groupOne == 2) {                //21:1
                var odds = oddsAftNinthPick[2];
                document.getElementById("group1").innerHTML;
                                  }
}
             if (groupOne == 3) {                //43:1
                var odds = oddsAftNinthPick[3];
                document.getElementById("group1").innerHTML;
                                  }
}
             if (groupOne == 4) {                //0:0
                var odds = oddsAftNinthPick[4];
                document.getElementById("group1").innerHTML;
                                  }
}
             if (groupTwo == 0) {                //10:1
                var odds = oddsAftNinthPick[0];
                document.getElementById("group2").innerHTML;
                                  }
}
             if (groupTwo == 1) {                //14:1
                var odds = oddsAftNinthPick[1];
                document.getElementById("group2").innerHTML;
                                  }
}
             if (groupTwo == 2) {                //21:1
                var odds = oddsAftNinthPick[2];
                document.getElementById("group2").innerHTML;
                                  }
}
             if (groupTwo == 3) {                //43:1
                var odds = oddsAftNinthPick[3];
                document.getElementById("group2").innerHTML;
                                  }
}
             if (groupTwo == 4) {                //0:0
                var odds = oddsAftNinthPick[4];
                document.getElementById("group2").innerHTML;
                                  }
}

//     This section below is just a note that describes how I want to compare the groups to the odds array.
//0    4/43   10:1    If var = 0 then there are 4 chances *.png in group* will show up. Therefore odds are 10:1
//1    3/43   14:1                          43 images left to choose from.
//2    2/43   21:1
//3    1/43   43:1
//4    0/43   0:0


This post has been edited by EdNolan: 30 October 2013 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: Adding up values from random arrray

Posted 31 October 2013 - 01:38 AM

if you want to output the result, youíll need to assign something to innerHTML ...
Was This Post Helpful? 0
  • +
  • -

#10 EdNolan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 241
  • Joined: 22-September 13

Re: Adding up values from random arrray

Posted 31 October 2013 - 06:34 PM

I tried this after doing some research but it's not working. Do I have to do this ... ("10:1") or something?
<table>
      <tr>
      <td><div id="group1"></div></td><td><div id="group2"></div></td><td><div id="group3"></div></td>
      </tr>
</table>


var groupOne = intersect(shownImages, ["1.png", "2.png", "3.png", "4.png"]).length;
var groupTwo = intersect(shownImages, ["5.png", "6.png", "7.png", "8.png"]).length;

function compare() { 
             if (groupOne == 0) {                //10:1
                document.getElementById("group1").innerHTML = "10:1";
                                  }

             if (groupOne == 1) {                //14:1
                document.getElementById("group1").innerHTML = "14:1";
                                  }

             if (groupOne == 2) {                //21:1
                document.getElementById("group1").innerHTML = "21:1";
                                  }

             if (groupOne == 3) {                //43:1
                document.getElementById("group1")innerHTML = "43:1";
                                  }

             if (groupOne == 4) {                //0:0
                document.getElementById("group1").innerHTML = "0:0";
                                  }

             if (groupTwo == 0) {                //10:1
                document.getElementById("group2").innerHTML = "10:1";
                                  }

             if (groupTwo == 1) {                //14:1
                document.getElementById("group2").innerHTML = "14:1";
                                  }

             if (groupTwo == 2) {                //21:1
//                var odds = oddsAftNinthPick[2];
                document.getElementById("group2").innerHTML = "21:1";
                                  }

//             if (groupTwo == 3) {                //43:1
//                var odds = oddsAftNinthPick[2];
//                document.getElementById("group2").textContent = "43:1";
//                                  }

//             if (groupTwo == 4) {                //0:0
//                var odds = oddsAftNinthPick[2];
//                document.getElementById("group2").innerHTML;
//                                  }
}


This post has been edited by EdNolan: 31 October 2013 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#11 EdNolan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 241
  • Joined: 22-September 13

Re: Adding up values from random arrray

Posted 31 October 2013 - 06:43 PM

Also tried.
                document.getElementById("group1").innerHTML = 10:1;


Was This Post Helpful? 0
  • +
  • -

#12 EdNolan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 241
  • Joined: 22-September 13

Re: Adding up values from random arrray

Posted 31 October 2013 - 06:51 PM

Okay, I know it's working now because the entire code that this is inside, is working. However I am not seeing the results of the odds on the screen. Here's what I did to get the innerHTML to work. Any idea why it's not being displayed with the <div></div>?

function compare() { 
             if (groupOne == 0) {                //10:1
               var odds = document.getElementById("group1");
               div.innerHTML = document.getElementById("10:1");
                                  }

             if (groupOne == 1) {                //14:1
                var odds = document.getElementById("group1");
               div.innerHTML = document.getElementById("14:1");
                                  }

             if (groupOne == 2) {                //21:1
                var odds = document.getElementById("group1");
               div.innerHTML = document.getElementById("21:1");
                                  }

             if (groupOne == 3) {                //43:1
                var odds = document.getElementById("group1");
               div.innerHTML = document.getElementById("43:1");
                                  }

             if (groupOne == 4) {                //0:0
                var odds = document.getElementById("group1");
               div.innerHTML = document.getElementById("0:0");
                                  }

             if (groupTwo == 0) {                //10:1
                var odds = document.getElementById("group2");
               div.innerHTML = document.getElementById("10:1");
                                  }

             if (groupTwo == 1) {                //14:1
                var odds = document.getElementById("group2");
               div.innerHTML = document.getElementById("14:1");
                                  }

             if (groupTwo == 2) {                //21:1
//                var odds = oddsAftNinthPick[2];
                var odds = document.getElementById("group2");
               div.innerHTML = document.getElementById("21:1");
                                  }

//             if (groupTwo == 3) {                //43:1
//                var odds = oddsAftNinthPick[2];
//                var odds = document.getElementById("group2");
//               div.textContent = document.getElementById("43:1");
//                                  }

//             if (groupTwo == 4) {                //0:0
//                var odds = oddsAftNinthPick[2];
//                var odds = document.getElementById("group2").innerHTML;
//                                  }
}


This post has been edited by EdNolan: 31 October 2013 - 07:04 PM

Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: Adding up values from random arrray

Posted 01 November 2013 - 02:17 AM

Quote

div.innerHTML = document.getElementById("10:1");

this is definitely not working.
- if you don’t have an ID of "10:1" in your document (which I strongly assume) the getElementById() call will return null, which is cast to an empty string => you see nothing.
- for the mere chance that there were such an ID existing, you’d get something along [object HTMLElement] (with the latter part being the HTMLDOM type of the element)


Quote

document.getElementById("group1").innerHTML = "10:1";

if this is not working
- check if you called the compare() function at all
- check if there exists an element with the ID of "group1"
- check the Error Console (F12)

This post has been edited by Dormilich: 01 November 2013 - 02:18 AM

Was This Post Helpful? 0
  • +
  • -

#14 EdNolan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 241
  • Joined: 22-September 13

Re: Adding up values from random arrray

Posted 01 November 2013 - 09:23 PM

WOW, there are so many ways to write innerHTML code. OMG I'm trying and trying. Does this look correct? It's still not showing anything. It should at least show 0:0 if anything. I have the script inside the <head></head> and am calling the function.

The error says I have a missing ; before statement on line 6, I don't understand that.It also says compare is not defined.

I don't know how else to define my compare function. Please help.

<div id="odds1"></div></td><td><div id="odds2"></div></td><td><div id="odds3"></div>

group1 = intersect(shownImages, ["1.png", "2.png", "3.png", "4.png"]).length;

function compareOdds() {
             if (group1 == 0) {                //10:1
                odds1 == 10:1;
                document.getElementById('odds1').innerHTML = "10:1";
                                  }

             if (group1 == 1) {                //14:1
                odds1 == 14:1;
                document.getElementById('odds1').innerHTML = "14:1";
                                  }

             if (group1 == 2) {                //21:1
                odds1 == 21:1;
                document.getElementById('odds1').innerHTML = "21:1";
                                  }

             if (group1 == 3) {                //43:1
                odds1 == 43:1;
                document.getElementById('odds1')innerHTML = "43:1";
                                  }

             if (group1 == 4) {                //0:0
                odds1 == 0:0;
                document.getElementById('odds1').innerHTML = "0:0";
                                  }
}

compareOdds()


This post has been edited by EdNolan: 01 November 2013 - 10:06 PM

Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: Adding up values from random arrray

Posted 02 November 2013 - 01:00 AM

Quote

I have the script inside the <head></head> and am calling the function.

no wonder you donít see anything. put it before the closing </body> tag.

besides that, what are lines #7, #12, #17, #22, #27 supposed to do? they would definitely trigger a syntax error.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2