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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts

    custom object methods question

    Hey all,

    This script below is just me trying to apply some of my knowledge of object oriented javascript but it's not returning an array, the result I expect it to return:

    Code:
    <script type="text/javascript">
    
    window.onload = function(){
    	alert(Aby.randomize(Aby.age););
    }
    
    function Teen(age){
    	this.age = age;
    }
    
    Aby = new Teen(19);
    
    Teen.prototype.randomize = function(randomizeAge){
    	var age = this.randomizeAge;
    	var count = 0;
    	var ageArray = [];
    	var num;
    	do {
    	num = Math.floor(Math.random() * this.age);
    		ageArray.push(num);
    	} while count < 4;
    	return ageArray
    }
    
    </script>
    Thanks for any response.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    where is count being updated with the array length?

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    this should throw an error at least. your while() syntax is incorrect.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    The variable count remains 0, thus your while() will enter in an infinite loop. You must increment it somewhere inside the loop.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    johnmerlino (06-24-2010)

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts
    There were two things wrong as stated. The while loop had incorrect syntax and the count++ increment. It appears sometimes firebug just doesn't work.


  •  

    Tags for this Thread

    Posting Permissions

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