11 Replies - 1188 Views - Last Post: 26 December 2010 - 09:02 PM

#1 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Some Array Function Troubles

Posted 23 December 2010 - 10:41 PM

This is too much. I've spent at least an hour staring at what feels like a brick wall which I can't do a damn thing to:

So, what we have here is an array full of words, right? All it is just a random list I wrote for a bingo board which I'm making. Simple. Beneath it is a while loop which is supposed to list all of the arrays. Now, this is located in the head tags of my index file. If you scroll down, I'll show you what else I have...

var words = new Array(
words[1] = "Religion"; 
words[2] = "Nietzche"; 
words[3] = "Inventor"; 
words[4] = "Visionary"; 
words[5] = "Philosophy"; 
words[6] = "Tesla"; 
words[7] = "AC>DC"; 
words[8] = "Sociology";
words[9] = "Parasite";
words[10] = "Parasite"; 
words[11] = "Hard"; 
words[12] = "Times"; 
words[13] = "HTML"; 
words[14] = "jQuery"; 
words[15] = "C++"; 
words[16] = "Develop"; 
words[17] = "Doctype"; 
words[18] = "Can"; 
words[19] = "You"; 
words[20] = "Find"; 
words[21] = "The"; 
words[22] = "Hidden";
words[23] = "Word?";
words[24] = "Verb"; 
words[25] = "Show"; 
words[26] = "There"; 
words[27] = "That"; 
words[28] = "Are"; 
words[29] = "Thou"; 
words[30] = "They"; 
words[31] = "Bloat"; 
words[32] = "ChimiChanga"; 
words[33] = "Gas"; 
words[34] = "Chess"; 
words[35] = "Strategy"; 
words[36] = "Psychology";
words[37] = "INTJ"; 
words[38] = "ENTJ"; 
words[39] = "INTP"; 
words[40] = "ENTP"; 
words[41] = "BLARGH"; 
words[42] = "So"; 
words[43] = "Many"; 
words[44] = "Different";
words[45] = "Words";
words[46] = "Just";
words[47] = "Cause";
words[48] = "Boss" ;
words[49] = "Bouss" ;
words[50] = "Interesting" ;
words[51] = "Make-Believe" ;
words[52] = "Hardship";
words[53] = "Challenge";
words[54] = "Laughing";
words[55] = "Creative";
words[56] = "Technology";
words[57] = "Humanity";
words[58] = "Genius";
words[59] = "Stoic";
words[60] = "Philosopher";
words[61] = "Ambitious";
words[62] = "Concentrated";
words[63] = "Conceptual";
words[64] = "Disciplined";
words[65] = "Half-Tracking";
words[66] = "Inventive";
words[67] = "Database";
words[68] = "Strange";
words[69] = "Success";
words[70] = "Psyche";
words[71] = "Understanding";
words[72] = "Aquarius";
words[73] = "Pisces";
words[74] = "Aries";
words[75] = "Taurus";
words[76] = "Gemini";
words[77] = "Cancer";
words[78] = "Leo";
words[79] = "Virgo";
words[80] = "Libra";
words[81] = "Scorpio";
words[82] = "Sagittarius";
words[83] = "Capricorn";
words[84] = "Personality";
words[85] = "Categorial";
words[86] = "Freak-show";
words[87] = "Nightmare";
words[88] = "Horror"; 
words[89] = "DieHard";
words[90] = "Ninety";
words[91] = "Tea";
words[92] = "Chopping";
words[93] = "Half-track";
words[94] = "Analytical";
words[95] = "Logic";
words[96] = "Odd";
words[97] = "Reason";
words[98] = "Business";
words[99] = "Innovation";
words[100] = "One-Hundred";
); 
  
  function postArray(){
  
    for(a=0; a<words.length; a++){
    
    document.write(words[a] + "</br>");
    
    }
  }



Beneath that, enclosed within a simple div tag, lies this:

<script type="text/javascript">
        
        document.write(postArray());
        
      </script>



Please, someone tell me, what am I doing wrong?

This post has been edited by macosxnerd101: 24 December 2010 - 03:16 PM
Reason for edit:: Title renamed.


Is This A Good Question/Topic? 0
  • +

Replies To: Some Array Function Troubles

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4405
  • View blog
  • Posts: 12,262
  • Joined: 18-April 07

Re: Some Array Function Troubles

Posted 23 December 2010 - 11:26 PM

You don't enclose all the items within the (). You set them outside.

var words = new Array(); //<-- Notice we are making words the array

words[0] = "Religion";  // <-- Then we set each item. Notice we are also starting at zero. Arrays start at index zero.
words[1] = "Nietzche"; 




That should fix up your problems. :)
Was This Post Helpful? 0
  • +
  • -

#3 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Some Array Function Troubles

Posted 24 December 2010 - 03:07 AM

Also, what are you doing by calling document.write() with postArray() as an argument? postArray _already_ does all the document.write()ing. Just call postArray() on its own. It probably will never cause any problems your way but it doesn't make any sense and you're just learning the wrong way.
Was This Post Helpful? 0
  • +
  • -

#4 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Re: Some Array Function Troubles

Posted 24 December 2010 - 01:25 PM

Ah, thanks muchos for the help. As far as the document.write goes, I honestly don't know what you mean by just "calling" it. I always thought with javascript you had to print your functions, like in php.

Edit: That is, unless you meant the postArray() which was within the postArray(). I honestly don't remember why I did that.

This post has been edited by litedrive: 24 December 2010 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#5 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 834
  • Joined: 25-October 08

Re: Some Array Function Troubles

Posted 24 December 2010 - 01:44 PM

Alternative array setup:
var WORDS = 'Religion Nietzche Inventor Visionary Philosophy Tesla AC>DC Sociology ';
    WORDS +='Parasite Parasite Hard Times HTML jQuery C++ Develop Doctype Can You Find '; 
    WORDS +='The Hidden Word? Verb Show There That Are Thou They Bloat ChimiChanga Gas Chess '; 
    WORDS +='Strategy Psychology INTJ ENTJ INTP ENTP BLARGH So Many Different Words Just ';
    WORDS +='Cause Boss Bouss Interesting Make-Believe Hardship Challenge Laughing ';
    WORDS +='Creative Technology Humanity Genius Stoic Philosopher Ambitious Concentrated ';
    WORDS +='Conceptual Disciplined Half-Tracking Inventive Database Strange Success Psyche ';
    WORDS +='Understanding Aquarius Pisces Aries Taurus Gemini Cancer Leo Virgo Libra Scorpio ';
    WORDS +='Sagittarius Capricorn Personality Categorial Freak-show Nightmare Horror DieHard Ninety ';
    WORDS +='Tea Chopping Half-track Analytical Logic Odd Reason Business Innovation One-Hundred ';
var words = WORDS.split(' ');


:)
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,613
  • Joined: 23-August 08

Re: Some Array Function Troubles

Posted 24 December 2010 - 02:04 PM

And arrays are indexed starting from 0, but you're not setting the 0th element of your array.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Some Array Function Troubles

Posted 24 December 2010 - 03:00 PM

that's not a problem for Javascript, the 0th value simply gets undefined assigned.
Was This Post Helpful? 0
  • +
  • -

#8 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Re: Some Array Function Troubles

Posted 25 December 2010 - 03:30 PM

I'll give both of those a shot, thanks.

One question: I'm trying to generate a randomized script of the words within that array in a table, kind of like a bingo board. I've been doing this for quite a while, and so far have come up with this:

var postArray =  function postArray(){
  
      for(a=0; a<words.length; a++){
    
      document.write(Math.round('words[a]')+"</br>");
      
      }
        
      var post_else = document.getElementByName('td').innerHTML = "fail";
      
      var post = document.getElementByName('td').innerHTML = words[a];
      
      if(document.innerHTML != words[a]){
        
      document.write(post);
      
      }
      
      else{
      
      document.write(post_else);
        
      }
 }


The rest is just a single <table> with a few <tr>, <td> elements inside of it, along with a set of separate script tags to print the functions and variables.

Any ideas on what I'm doing wrong? I'm thinking it must be the innerHTML function: the "=" may not be the proper way to specify the replacement text, though that's what I saw when using a tutorial. Anyway, any thoughts?
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Some Array Function Troubles

Posted 25 December 2010 - 04:25 PM

there is no function document.getElementByName(), that would be document.getElementsByName() but in your case it's rather document.getElementsByTagName(). bear in mind that this returns a list, not a single element.

additionally, .innerHTML is read-only in IE on table elements.
Was This Post Helpful? 0
  • +
  • -

#10 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Re: Some Array Function Troubles

Posted 26 December 2010 - 03:26 PM

I see, I see...

Here's the question, then: which function and script should I create to post a single randomized word out of the array in multiple elements which all have the same html tag? Should I take out the increment in the top function I posted?

At the moment all I'm really doing is messing around with some for scripts, using Math.random/Math.round functions to generate random individual words, and so far nothing has really worked.

This post has been edited by litedrive: 26 December 2010 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#11 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 834
  • Joined: 25-October 08

Re: Some Array Function Troubles

Posted 26 December 2010 - 08:37 PM

I'm still not sure I understand the requirements,
but here is a start of a script you can modify.

Possible modifications ...
1. You can add more Bingo boards.
2. When a word is CHOSEN, highlight that word in each board.
3. Remove possibility of displaying duplicated CHOSEN words.
4. Change background color of each Bingo board.
5. Maintain a list of the CHOSEN words to check against winning board.
6. Fairly easy to change words in Bingo board and selections
7.
<html>
<head>
<title> Word Bingo </title

<script type="text/javascript">

var WORDS = 'Religion Nietzsche Inventor Visionary Philosophy Tesla AC>DC Sociology ';
    WORDS +='Parasite Parasite Hard Times HTML jQuery C++ Develop Doctype Can You Find '; 
    WORDS +='The Hidden Word? Verb Show There That Are Thou They Bloat ChimiChanga Gas Chess '; 
    WORDS +='Strategy Psychology INTJ ENTJ INTP ENTP BLARGH So Many Different Words Just ';
    WORDS +='Cause Boss Bouss Interesting Make-Believe Hardship Challenge Laughing ';
    WORDS +='Creative Technology Humanity Genius Stoic Philosopher Ambitious Concentrated ';
    WORDS +='Conceptual Disciplined Half-Tracking Inventive Database Strange Success Psyche ';
    WORDS +='Understanding Aquarius Pisces Aries Taurus Gemini Cancer Leo Virgo Libra Scorpio ';
    WORDS +='Sagittarius Capricorn Personality Categorial Freak-show Nightmare Horror DieHard Ninety ';
    WORDS +='Tea Chopping Half-track Analytical Logic Odd Reason Business Innovation One-Hundred';
var words = WORDS.split(' ');

function $_(IDS) { return document.getElementById(IDS); }

var Board0_Ans = [];
var Board1_Ans = [];
var Board2_Ans = [];
var Board3_Ans = [];
var Board4_Ans = [];
var Board5_Ans = [];

function GenerateBoard(Bno) {
  var Board = [];
  for (var i=0; i<25; i++) {
    var Word = words[Math.floor(Math.random() * words.length)];
    Board.push(Word);
  }  // alert(Board.join('\n'));
  switch (Bno) {
    case 1: Board1_Ans = Board.slice(0);
    case 2: Board2_Ans = Board.slice(0);
    case 3: Board3_Ans = Board.slice(0);
    case 4: Board4_Ans = Board.slice(0);
    case 5: Board5_Ans = Board.slice(0);
  }
}

function DrawBoard(Bno) {  
  var Board = [];
  switch (Bno) {
    case 1: Board = Board1_Ans;
    case 2: Board = Board2_Ans;
    case 3: Board = Board3_Ans;
    case 4: Board = Board4_Ans;
    case 5: Board = Board5_Ans;
  }
  var str = '';
  str += '<table border="1" width="50%">';
  for (var r=0; r<5; r++) {
    str += '<tr>';
    str += '<th>'+Board.slice(r*5,(r*5)+5).join('</th><th>')+'</th>';
    str += '</tr>';
  }
  str += '</table>';
  $_('Board'+Bno).innerHTML = str;
}

function ChooseWord() {
  alert(words[Math.floor(Math.random() * words.length)]);
}
</script>

</head>
<body>
<button onclick="GenerateBoard(1);DrawBoard(1)">Make Board 1</button>
<button onclick="GenerateBoard(2);DrawBoard(2)">Make Board 2</button>
<button onclick="GenerateBoard(3);DrawBoard(3)">Make Board 3</button>
<button onclick="GenerateBoard(4);DrawBoard(4)">Make Board 4</button>
<button onclick="GenerateBoard(5);DrawBoard(5)">Make Board 5</button>
<br><button onclick="ChooseWord()">Choose Word</button>

<p><div id="Board1"></div>
<p><div id="Board2"></div>
<p><div id="Board3"></div>
<p><div id="Board4"></div>
<p><div id="Board5"></div>

</body>
</html>


Have fun with it.
:bananaman:

This post has been edited by Dormilich: 27 December 2010 - 03:51 AM

Was This Post Helpful? 0
  • +
  • -

#12 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Re: Some Array Function Troubles

Posted 26 December 2010 - 09:02 PM

Whoa. Thank you, sir. Thank you very, very much. I will put this to good use, I assure you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1