Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function breaks and mozillas Javascript console has nothing to say

    Hi. I am producing a word cloud however changing the size of the words is proving to be a confusing problem.

    basically there is a problem with this ...


    Code:
    for(i=0; i<words.length; i++){
    
    	wordCloud = wordCloud + "<span style='font-size:" + freqOfWord(words[i],words) + "em'>" + words[i] + "</span> ";
    
    }
    where...

    'words' is the name of the array that stores all of the input words.

    'wordcloud' is the variable that holds the html for the word cloud.

    'freqOfWord(word,arrayName) is a function that loops through the array and returns the number of occurences of that word.

    Code:
    function freqOfWord(word,wordArray){
    	number = 0;
    	for(i=0; i<wordArray.length; i++){
    		if(word==wordArray[i]){
    			number++;
    		}
    	}
    	alert(number);
    	return number;
    }
    Now for some reason the loops breaks after the first run and the value of 'wordCloud' is set to 'Undefined' despite the fact that mozillas javascript console has nothing to say.

    Yet if I change the wordCloud line to the following then it loops through fine and the variable wordCloud has all of the correct data in it except for the sizes.

    Code:
    	wordCloud = wordCloud + "<span style='font-size:" + 1 + "em'>" + words[i] + "</span> ";
    I don't understand why it won't take the size that is being returned by the freqOfWord() function so please any help would be great.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    In function freqOfWord you are using the variable i when it is also being used in the words.length loop, try changing the i in freqOfWord to j see if that helps

    for(j=0; j<wordArray.length; j++){
    if(word==wordArray[j]){
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Mr J. Can't believe I never picked up on that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •