0

I am building an array that will host objects with a student's name, their grade and z score (currently value is set to null) but am having trouble loading the objects.

The object is meant to look like this:

var 000 = {
    name: "John Doe",
    grade: 92,
    zScore: null
};

Fiddle here.

HTML:

<div id="content">
    <input type="text" id="name" placeholder= "Name . . .">
    <input type="text" id="grade" placeholder = "Grade . . . ">
    <button id="add">Add</button>
    <button id="doMath">Do Math</button>
    <br>
    <br>
    <ul id="list"></ul>
</div>

CSS:

#name:hover,#grade:hover {
    border: 1px solid #000;
}

JavaScript:

var studentArray = [];
var oid = 001;
var studentName = $('#name').val();
var studentGrade = (parseInt($('#name').val()));
var zScore = null;
$(document).ready(function() {
    $('#add').on('click', function() {
        if (studentName === "" || studentGrade === "") {
            return;
        };
        oid = {
            name: studentName,
            grade: studentGrade,
            z: zScore
        };
        //check it out
        console.log(oid);
        //push object to array
        studentArray.push(oid);
        //check out array
        console.log(studentArray);
        //push OID up for next student and reset inputs
        $('#name').val("");
        $('#grade').val("");
        oid++;
    });
    $('#doMath').on('click', function() {});
});

What am I doing wrong?

4
  • 2
    var 000? does that work? Commented Oct 27, 2015 at 18:14
  • That's a sample. var oid actually starts at 001 Commented Oct 27, 2015 at 18:17
  • uh, oid starts as a number, then you assign an object to it, then you try to increment that object...won't work! Commented Oct 27, 2015 at 18:18
  • you're setting the value of student grade with #name and not #grade. Also you'll need to grab those values within your click function otherwise they will always be empty Commented Oct 27, 2015 at 18:23

2 Answers 2

1

by adding to an array you're essentially disregarding the object name. Perhaps instead you could use a parent object?

var studentObj = {};
var oid = 001;
var studentName = "evan";
var studentGrade = "a";
var zScore = null; 
//push to object
studentObj[oid] = {
    name: studentName,
    grade: studentGrade,
    z: zScore
};
//check out object
console.log(studentObj);
//push OID up for next student and reset inputs
$('#name').val("");
$('#grade').val("");
oid++;

https://jsfiddle.net/kogx7Lks/3/

Sign up to request clarification or add additional context in comments.

7 Comments

In the code there is studentArray[oid] = { name: studentName, grade: studentGrade, z: zScore but when I open up the console, grade comes before student. Why is this?
whats the end goal? how are you planning on displaying the data?
Isn't displaying it alphabetically confusing? I am planning on accessing the score property to eventually calculate the z score property. The end goal is to display z scores. I have not yet decided how to display the data yet but probably a list for now with the name, grade and z score.
I added a quick function for #doMath click at the bottom, take a look jsfiddle.net/kogx7Lks/6
I actually have not seen the hasOwnProperty method yet so I looked it up. Am I correct in understanding that what it is doing is accessing the properties by name? E.g. studentArray[property].grade gets the grade but couldn't you use studentArray.grade ?
|
1

see this code it help you you assign value every time when click:

$('#add').on('click', function() {
    studentName = $('#name').val();
    studentGrade = (parseInt($('#name').val()));
    ...
 });

http://jsfiddle.net/kogx7Lks/4/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.