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 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Learning javascript, Help

    Code:
    <HTML>
    
    <HEAD>
    
    <SCRIPT TYPE="text/javascript">
    
    //Define Functions
    function student(name,age,mother) {
    this.name = name;
    this.age = age;
    this.mother = mother;
    this.displayProfile = displayProfile;
    }
    
    function grade(math,english,science) {
    this.math = math;
    this.english = english;
    this.science = science;
    }
    
    function displayProfile() {
    document.writeln("Name: " + this.name + ");
    document.writeln("Age: " + this.age + ");
    document.writeln("Mother's name: " + this.mother + ");
    document.writeln("Math Grade: " + this.grade.math + ");
    document.writeln("English Grade: " + this.grade.english + ");
    document.writeln("Science Grade: " + this.grade.science + ");
    }
    </SCRIPT>
    
    </HEAD>
    
    <BODY>
    
    <SCRIPT TYPE="text/javascript">
    {
    student1 = new student("John",18,"Diane");
    johngrade = new grade(100,80,90);
    student1.displayProfile();
    }
    </SCRIPT>
    </BODY>
    </HTML>
    Alright, I am learning javascript and in the middle of experimenting with functions, I don't see why this does not work?

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <HTML>
    
    <HEAD>
    
    <SCRIPT TYPE="text/javascript">
    
    //Define Functions
    function student(name,age,mother) {
    	this.name = name;
    	this.age = age;
    	this.mother = mother;
    	this.displayStudent = function(){displayStudent.call(this)};
    }
    
    function grade(math,english,science) {
    	this.math = math;
    	this.english = english;
    	this.science = science;
    	this.displayGrade = function(){displayGrade.call(this)};
    }
    
    function displayStudent() {
    	document.writeln("Name: " + this.name );
    	document.writeln("Age: " + this.age );
    	document.writeln("Mother's name: " + this.mother );
    }
    function displayGrade() {alert('d')
    	document.writeln("Math Grade: " + this.math );
    	document.writeln("English Grade: " + this.english );
    	document.writeln("Science Grade: " + this.science );
    }
    
    </SCRIPT>
    
    </HEAD>
    
    <BODY>
    
    <SCRIPT TYPE="text/javascript">
    {
    student1 = new student("John",18,"Diane");
    johngrade = new grade(100,80,90);
    student1.displayStudent();
    johngrade.displayGrade();
    }
    </SCRIPT>
    </BODY>
    </HTML>

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For the challenge I must only use one function to output it all, not one for grade and one for info. It must be all in 1 function. (Doing this as an assignment)

    Also, I note that you added
    Code:
    this.displayGrade = function(){displayGrade.call(this)};
    Isn't the function() { } not needed? only the displayGrade.call(this) ??

    And what does the displayGrade.call(this) exactly do? My instructor has not taught us .call yet so I think there must be a way to do it without it?

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    [QUOTE=TehMadHatter;1181951]For the challenge I must only use one function to output it all, not one for grade and one for info. It must be all in 1 function. QUOTE]

    Well sorry, I guess this won't help.

    Code:
    //Define Functions
    function student(name,age,mother) {
    	this.name = name;
    	this.age = age;
    	this.mother = mother;
    	this.display = function(){display.call(this)};
    }
    
    function grade(math,english,science) {
    	this.math = math;
    	this.english = english;
    	this.science = science;
    	this.display = function(){display.call(this)};
    }
    
    function display() {
        for(var a in this)
    	if(typeof this[a] != "function")
    		document.write(a+":"+this[a]+"<br>");
    }
    
    </SCRIPT>
    
    </HEAD>
    
    <BODY>
    
    <SCRIPT TYPE="text/javascript">
    {
    student1 = new student("John",18,"Diane");
    johngrade = new grade(100,80,90);
    student1.display();
    johngrade.display();
    }
    maybe like this ...


    Code:
    //<HTML>
    
    <HEAD>
    
    <SCRIPT TYPE="text/javascript">
    
    //Define Functions
    function student(name,age,mother) {
    	this.name = name;
    	this.age = age;
    	this.mother = mother;
    	this.display = function(){display(this)};
    }
    
    function grade(math,english,science) {
    	this.math = math;
    	this.english = english;
    	this.science = science;
    	this.display = function(){display(this)};
    }
    
    function display(that) {
        for(var a in that)
    	if(typeof that[a] != "function")
    		document.write(a+":"+that[a]+"<br>");
    }
    
    </SCRIPT>
    
    </HEAD>
    
    <BODY>
    
    <SCRIPT TYPE="text/javascript">
    {
    student1 = new student("John",18,"Diane");
    johngrade = new grade(100,80,90);
    student1.display();
    johngrade.display();
    }
    </SCRIPT>
    </BODY>
    </HTML>
    Last edited by DaveyErwin; 01-17-2012 at 10:34 PM.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,023
    Thanks
    56
    Thanked 567 Times in 564 Posts
    like so?

    Code:
    <HTML>
    
    <HEAD>
    
    <SCRIPT TYPE="text/javascript">
    
    //Define Functions
    function student(name,age,mother) {
    this.name = name;
    this.age = age;
    this.mother = mother;
    this.displayProfile = displayProfile;
    this.grade = new grade(100,80,90);
    }
    
    function grade(math,english,science) {
    this.math = math;
    this.english = english;
    this.science = science;
    }
    
    function displayProfile() {
    document.writeln("Name: " + this.name);
    document.writeln("Age: " + this.age);
    document.writeln("Mother's name: " + this.mother);
    document.writeln("Math Grade: " + this.grade.math);
    document.writeln("English Grade: " + this.grade.english);
    document.writeln("Science Grade: " + this.grade.science);
    }
    </SCRIPT>
    
    </HEAD>
    
    <BODY>
    
    <SCRIPT TYPE="text/javascript">
    {
    student1 = new student("John",18,"Diane");
    student1.displayProfile();
    }
    </SCRIPT>
    </BODY>
    </HTML>

  • #6
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    I was able to complete it myself, this is what I was looking for:

    Code:
    <HTML>
    
    <HEAD>
    
    <SCRIPT TYPE="text/javascript">
    
    //Define Functions
    function student(name,age,grade) {
    	this.name = name;
    	this.age = age;
    	this.grade = grade;
    	this.display = display;
    }
    function grade(math,english,science) {
    	this.math = math;
    	this.english = english;
    	this.science = science;
    }
    	
    function display() {
    	document.writeln("Name: " + this.name + "<BR /> Age: " + this.age + "<BR />");
    	document.writeln("Math grade: " + this.grade.math);
    	document.writeln("<BR />English grade: " + this.grade.english);
    	document.writeln("<BR />Science grade: " + this.grade.science);
    }
    
    </SCRIPT>
    
    </HEAD>
    
    <BODY>
    
    <SCRIPT TYPE="text/javascript">
    {
    	johngrade = new grade(95,80,100);
    	student1 = new student("John",18, johngrade);
    	student1.display();
    }
    </SCRIPT>
    </BODY>
    </HTML>

    Thanks though.

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by TehMadHatter View Post
    And what does the displayGrade.call(this) exactly do?
    call() is a method of the Function object (i.e. every function as instance of Function has this method), that allows to change the scope of the function (what you access by the keyword this).

    ex. walk through a list of elements jQuery-like
    Code:
    var a = document.getElementsByTagName("a");
    [].forEach.call(a, function(item, index, obj) {
        item.onclick = doSomething;
    });
    ex. remove the first 2 parameters from a function call
    Code:
    function test()
    {
        // arguments is an object holding the call parameters
        var args = [].splice.call(arguments, 0, 2);
        alert(args.join(", "));
    }
    test(1, 2, 3, 4, 5);
    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

  • #8
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    !! thanks you guys...from the 4th time at last I could make it correctly...now it works!



    _____________________
    free photo editor


  •  

    Posting Permissions

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