My mission is to solve this:
Write a function that will take an array of numbers, and will return the average of all numbers in the array that are prime number.
So far I wrote this:
function average(a){
var arr=[];
var newarr=[];
var i,j,sum,sum1,average,counter,counter1;
counter=counter1=sum=sum1=0;
for(i=0;i<a.length;i++){
for(j=2;j<(Math.floor(a[i]/2));j++){
if((a[i]%j===0) && (a[i]>2)){
arr.push(a[i]);
break;
}
}
}
for(i=0;i<a.length;i++){
sum=sum+a[i];
counter++;
}
for(i=0;i<arr.length;i++){
sum1=sum1+arr[i];
counter1++;
}
average=(sum-sum1)/(counter-counter1);
return average;
}
var a=[88,44,32,30,31,19,74,169,143,109,144,191];
console.log(average(a));
I am allowed to use only: conditions (if
), loops, --
, ++
, %
, /
, *
, -
, +
, ==
, =!
, =>
, >
, =<
, <
, ||
, &&
, =%
, =/
, =*
, +-
, =+
, array.length
, array.pop()
and concat
.
Any suggestions? Feedback on what I wrote? Thank you!
=!
,=+
and many others are not operators in JavaScript. Can you please double-check the description? (I suspect these should be!=
and+=
, respectively. There are several other odd ones too.) Also, I assume you can also usefunction
, since you are in fact using it. Right? \$\endgroup\$