0

Hi can someone guide me how to write these code in oop?

<script type="text/javascript">
    return answer;
    var rslt=0;
    var answer= "";
    var a,b,c;
    var fill = new Array(50);

    function Random(min,max)
        {return Math.floor(Math.random() * (max - min + 1)) + min;}

    function myTwoDArray(){
        var string="";
        var myArray= new Array (20);
        var position=0;
        fillarrayindex(1);
        for(var i=0;i<10;++i){
            myArray[i]=new Array(20);

            for(var j=0;j<10;++j){
                if(fill[position]==0){  
                    string=badRandom(); 
                    position++;
                    myArray[i][j]=1;
                }
                else{

                    string=goodRandom();
                    position++;
                    myArray[i][j]=2;
                }

            }
        }}}

    function fillarrayindex(j){
        for (i=0;i<y;i++){
            no = getRandom(0,20); 
            fill[no]=1;

        }
        return fill;
    }


    function goodrandom ()
    {
        a = Random(0,1000);
        b = Random(1,100);
        c = Random(200,300);

        if ((a%2)==0){
            rslt = b+c;
            answer =b+ c + result;
            return answer;

        }
        else{   
                rslt = b-c;
                answer = b + c +result; 
                return answer;
        }

    }

    function badrandom(){
        a = Random(0,1000);
        b = Random(1,100);
        c = Random(200,300);

        var badNum = getRandom(0,10);
        result= a+b;


        if (badNum==result){badNum++;}
        if ((a%2)==0){  
            answer = b + c + badNum;
            return answer;
        }
        else{
            answer = b + c + badNum;
            return answer;
        }

    }

</script>
3
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by remunance
2

JavaScript is very dynamic, so you can use OOP in various ways. Take a look at a couple of examples:

// -------------------------
// Using Prototype
var MyPrototype = function () {
    // do something
};

MyPrototype.prototype.MyMethod1 = function() {
    return this.MyMethod2();
};

MyPrototype.prototype.MyMethod2 = function() {
    return "something";
};

// Usage
var myObj = new MyPrototype();
myObj.MyMethod2();

// -------------------------
// Using a object like an "static" class

var MyStaticObject = {

    MyMethod1: function() {

        return MyStaticObject.MyMethod2();

    },

    MyMethod2: function() {
        return "Something";
    }

};

// Usage
MyStaticObject.MyMethod1();

For futher understading, read those pages:
https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript

http://killdream.github.com/blog/2011/10/understanding-javascript-oop/

Votes + Comments
Nice post
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.