Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript .push is creating new arrays!?

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #163717
    bcintegrity1
    Participant

    I currently have a star rating dynamically created/displayed with each member’s listing. I’m now trying to create an average rating for all members on the page by using .push.

    My current code sums each rating with the previous one, and then creates a new object array for each result.

    //create empty array
    var arr = [];
    
    //perform task for each listing
    $('.lbox').each(function(event) {
    
    //get review file
    var name = $(this).find("h2.first").text();
    name = name.split(/^(\w+)\s+(\w+)/);
    var firstName = name[1];
    var lastName = name[2];
    var folder = "reviews-pest-control-va";
    var reviewsPath = folder + '/' + lastName + ',' + firstName + '.php';
    var reviewsFile = reviewsPath.toLowerCase();
    var rating = $(this).find('.rating');
    
    //get average rating
    $.get(reviewsFile, function(data){
    var $reviewTotal = $(data).find(".review").length;
    if ($reviewTotal < 1){rating.text("No Reviews Submitted");}
    else{
    var $star5 = ($(data).find(".five").length) * (5);
    var $star4 = ($(data).find(".four").length) * (4);
    var $star3 = ($(data).find(".three").length) * (3);
    var $star2 = ($(data).find(".two").length) * (2);
    var $star1 = ($(data).find(".one").length) * (1);
    var $reviewAvg = ($star5 + $star4 + $star3 + $star2 + $star1) / ($reviewTotal);
    
    //display rating
    rating.append("AVERAGE RATING: " + $reviewAvg.toFixed(1));
    var middle = $(rating).parent().find(".middleimage");
    var $forPix = ($reviewAvg) * (12.8);
    var $pixWidth = $forPix.toFixed(0);
    middle.css({"width": $pixWidth});}
    
    //my attempt to push each member's average into array
    arr.push($reviewAvg);
    var totalReviews = 0;
    $.each(arr,function(){totalReviews+=this;});
    
    //web console shows an object array for each member
    console.log(arr);
    
    //Will be used to get avg. from combined members
    var $pageAvg = totalReviews;
    
    //Similar rating/star display as with each member's listing
    $('.c_rating').append($pageAvg.toFixed(1)+",");
    var middlePic = $('.c_rating').parent().find(".c_middleimage");
    var $forPixs = ($pageAvg) * (20.608);
    var $pixsWidth = $forPixs.toFixed(0);
    middlePic.css({"width": $pixsWidth});
    });});
    

    To see my headache in action, here’s my test page. http://integritycontractingofva.com/testpage.php

    Thanks in advance for any help!

    #163775
    bcintegrity1
    Participant

    Just to clarify, the .push function currently inserts into an array, sums the numbers, but then places the total into a new array.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.