arrays - Total cost function in javascript -
arrays - Total cost function in javascript -
i having issues (very lost) in making code me , friend given create.
so suppose compute , homecoming average of values in given array named customerbalance, array holds amount of "what customers owe business" (i dont own business) , each item in array holds "customers balance", have utilize for() process array , calculate average , split customerbalance length, , homecoming average.
here code far
function average() { customerbalance for(i=0,i++) sum(customerbalance) total=sum/5 homecoming average;
i know wrong, not sure on how start typing array, please don't harsh know how this.
thank , have great day
function average(customerbalance) { if (customerbalance.length == 0) { // prevent partition 0 later homecoming 0; } var total = 0; (var = 0; < customerbalance.length; i++) { total += customerbalance[i]; } var average = total/customerbalance.length; homecoming average; }
you have many problems:
the parameter function goes in parenthese after function name, not next line. yourfor()
syntax wrong. need set initialization, repetition test, , increment separated semicolons. there's no sum()
function in javascript. , if there were, need assign result variable. when calculate average, you're putting in total
, you're returning average
, variable contains function, not average calculated. other recommendations:
don't hard-code array size, utilizearray.length
it. always set braces around body of for
, if
, while
, etc. if they're 1 line. local variables should declared var
. javascript arrays
Comments
Post a Comment