0

I have been searching for a way to process my data. I have tried Arrays, Objects and JSON.stringify() but I can't seem to succeed with thoose.

I need a code to process this type of data

first object:
first data {lesson: "MATH", count: 6}
second data {lesson: "BIOLOGY", count: 4}

second object:
first data {lesson: "MATH", average: 85, period: 1}
second data {lesson: "BIOLOGY", average: 40, period: 1}

third object
first data {lesson: "MATH", average: 95, period: 2}
second data {lesson: "BIOLOGY", average: 45, period: 2}

I need two function to populate objects with the given data and call them when need like

   function reqDataAverage(lesson, period) {
   //some stuff todo
   return average;
}
   function reqDataCount(lesson) {
   //some stuff todo
   return count;
}

I'm creating data with this code (Note: element is "first data".)

var element = new Object();
var x;
console.log("Start of data");
for (x in TArrayI) {
    var first = TArrayI[x][0];
    var second = TArrayI[x][averageRow];
    if (first == "") {
        break;
    } else {
        element.lesson = first;
        element.average = second;
        element.period = 1;
        console.log("Lesson is " + element.lesson + "and average is " + element.average + "/" + element.period);
    }
}

After this is complete I must able to do something like this:

var lessons = [...]
var result = 0;
var sum;
    function math() {
    var x;
        for (x in lessons) {
    var lesson = lessons[x];
    var averageOfThis = reqDataAverage(lesson, 1);
    var multiplierCount = reqDataCount(lesson);
    var result = result + (averageOfThis x multiplierCount);
       }
       var sum = result / 40;
    }

Best Regards

  • First object is number of students or tests and and second and third are average results? It looks like two kinds of objects which may be confusing things. I suggest an array of objects, each of which will contain an array of subsequent objects based on what I see here. – Jon Edwards Jun 09 '15 at 17:12
  • Okay now, I will use push() to push element to array. But how do I get the specific value I want? – Deniz Ugur Jun 09 '15 at 17:15
  • For each property in the object, you can use reduce the array. Something like sum = lesson[math].results.reduce(function(prev, curr){return prev + curr; }, and then divide that by lesson[math].results.length if you wanted to create a quasi-array that has strings as keys. I'm not sure how many lessons you'll have and how you'll want to use them so some considerations will have to be made there. – Jon Edwards Jun 09 '15 at 17:20
  • Can you make a function of it like I mentioned above? – Deniz Ugur Jun 09 '15 at 17:36

1 Answers1

0

I think I understand what you're asking for. You'll need to loop through with the findAndAdd() function to populate your averages arrays.

var lessons = [];

function findAndAdd(subject, period, average){
  lessons.forEach(function(lesson){
    if(lesson.subject === subject && lesson.period === period){
      lesson.averages.push(average);
    }
  });
  return -1;
}

function Lesson(subject, period){
  this.subject = subject;
  this.period = period;
  this.averages = [];
}

function count(subject, period){
  var cnt = -1;
  lessons.forEach(function(lesson){
    if(lesson.subject === subject && lesson.period === period){
      cnt = lesson.averages.length;
    }
  });
  return cnt;
}

function average(subject, period){
  var sum, lesson;
  lessons.forEach(function(lsn){
    if(lsn.subject === subject && lsn.period === period){
      lesson = lsn;
    }
  });
  if(lesson.averages.length > 0){
    sum = lesson.averages.reduce(function(prev, curr){
      return prev + curr;
    });
    return sum / lesson.averages.length;
  }
  return -1;
}

// sample data for testing purposes
var newObj = new Lesson('MATH', 1);
lessons.push(newObj);
newObj = new Lesson('MATH', 2);
lessons.push(newObj);
newObj = new Lesson('BIOLOGY', 1);
lessons.push(newObj);
newObj = new Lesson('BIOLOGY', 2);
lessons.push(newObj);

findAndAdd('MATH', 1, 65);
findAndAdd('MATH', 1, 75);

console.log(count('MATH', 1));
console.log(average('MATH', 2));
Jon Edwards
  • 177
  • 1
  • 8
  • I'm trying to implement your code into mine. If it succeeds I will approve your question. – Deniz Ugur Jun 09 '15 at 22:02
  • Cool. Let me know if you have any questions or something doesn't make sense. – Jon Edwards Jun 09 '15 at 22:05
  • Just a quick question where did "lesson" came from. I don't seem to understand. The one in lessons.forEach(function(lesson){}); – Deniz Ugur Jun 09 '15 at 22:42
  • Also I was using a wrong code to push objects to array. Your code helped me to fix them too. – Deniz Ugur Jun 09 '15 at 22:43
  • forEach is an iterator that takes successive items out of an array and presents them to the anonymous function through each iteration. It's really handy because you can use it as a replacement for 'for' and not worry about the iterator variables. It does all the work for you. If you're relatively unfamiliar with the Array.prototype methods, I highly recommend getting to know them. They are incredibly handy. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype And I'm glad to hear I helped with the other issue! – Jon Edwards Jun 09 '15 at 22:45
  • Can you check out my script on github. I implemented your code but mathAverage() logs both return undefined. If you can help me fix the undefined, my codes %80 would be finished. – Deniz Ugur Jun 09 '15 at 22:51
  • Yup. The return in the forEach loop is returning back to the function from forEach, not from the function to the calling function. Assign the value to a variable with function scope and return that value at the end of the function (like I did in the code I wrote above). – Jon Edwards Jun 09 '15 at 22:57
  • Are there values within the functions? That is to say, if you add a console log prior to the return, are you seeing the behavior you would expect? – Jon Edwards Jun 09 '15 at 23:02
  • console log is only for test. See at the end I would be able to get average of a specific lesson easily. Same for lesson count. I don't know much about java but this is the last part so I need your all help for this. – Deniz Ugur Jun 09 '15 at 23:09
  • Put a console log right before the return: function LessonCountify(subject){ var countval; HDPObj.forEach(function(LessonCount){ if(LessonCount.subject === subject) { var countval = LessonCount.count; } }); ^^^^ before this line return countval; } – Jon Edwards Jun 09 '15 at 23:18
  • Hey, I fixed the problem but I need another simple help – Deniz Ugur Jun 09 '15 at 23:53
  • if you look to my script there is a countStrings(table) function. At the end of that function I push the newObj to my desired array but If I call it in mathAverage() function it comes unreadable. I can show this via console output. [Imgur link for console output](http://imgur.com/tLbNBvb) – Deniz Ugur Jun 09 '15 at 23:59
  • I'm starting to get a little beyond my expertise, but I think we need a deep compare of the names since we're using UTF8 characters. In other words, a simple string comparison may not longer be working. You may actually have to iterate through your strings character by character to make sure that they match. It looks like there's something called 'localCompare' that I haven't needed before but may help you here. http://stackoverflow.com/questions/3630645/how-to-compare-unicode-strings-in-javascript – Jon Edwards Jun 10 '15 at 00:03
  • Thank you for your support code is finished. I just need to clear it. And it wasn't a deep compare I just need to do following: var x = this.word.toString(); var newObj = new LessonCount(x, this.count); – Deniz Ugur Jun 10 '15 at 00:32