I have a coding challenge in which I need to create some sort of an average calculator. As it's a beginner's course, the challenge is quite straightforward ( just input a bunch of variables and add them ). However I tried to use a for loop to make my life easier.
It keeps giving me the "NaN" answer when I console log it.
I don't really know what's wrong here. It seems relatively logical from my noob perspective. I tried putting the average var inside the for loop, but it would just average the 1st and 2nd, then move onto 2nd and 3rd, and then finally give me NaN again.
var johnTeam, mikeTeam;
var johnAverage,mikeAverage;
johnTeam = [89,120,103];
mikeTeam = [116,94,123];
function averageCalc(){
var i;
for (i in johnTeam){
var j=i++;
}
var average=(johnTeam[i]+johnTeam[j])/3;
console.log(average)
}
Expected result should be '104'.
Current result 'NaN'.
You'd never use for in to loop an array, you usually don't use i after the loop. Also, i in your case is always only the last counter value. To get the sum of an array, you'd usually use reduce and to get the average you'll divide earlier result by length
const johnTeam = [89,120,103];
const mikeTeam = [116,94,123];
const average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
const johnAvg = average(johnTeam);
const mikeAvg = average(mikeTeam);
console.log(johnAvg);
console.log(mikeAvg);
Also consider using this :
var sumjohnTeam = 0;
johnTeam.forEach(p => (sumjohnTeam += p));
var avgjohnTeam= sumjohnTeam /johnTeam.length;
The arithmetic mean is computed by taking the sum of all the measurements and dividing by the total number of measurements. If you want to be a good statistician, null/undefined measurement counts toward N, the number of measurements, but not towards the sum of all measurements. That gives us this:
function mean( values ) {
if (!values || !value.length) return undefined;
let sum = 0;
for ( let i = 0 ; i < values.length ; ++i ) {
const value = values[i];
if (value == undefined || value == null) continue;
sum += value ;
}
return sum / values.length ;
}
With that, it's easy:
const johnTeam = [89,120,103];
const johnAverage = mean( johnTeam ) ;
const mikeTeam = [116,94,123];
const mikeAverage = mean( mikeTeak ) ;
You just need to calculate the sum in the loop then outside you divide by the number of elements in you array.
Here's an updated version of your try :
var johnTeam = [89, 120, 103],
mikeTeam = [116, 94, 123],
johnAverage = 0,
mikeAverage = 0;
// added a parameter so you can send the team array you'll want to calculate its average.
function averageCalc(teamArray) {
// stores the sum.
var sum = 0;
// loop and calculate the sum.
// notice the teamArray[i] not only i.
for (var i in teamArray) sum += teamArray[i];
// calculate average based on the array length and logs it out.
console.log(sum / teamArray.length);
}
// tests
averageCalc(johnTeam); // 104
averageCalc(mikeTeam); // 111
Related
edit: without giving me too much of the answer to how i can do it in a for loop. Could you give me the logic/pseudocode on how to achieve this? the one part i am stuck at is, ok i know that i have to take the first index number and add array.length-1 zeros to it (2 zeros), but i am confused as to when it arrives at the last index, do i put in a if statement in the for loop?
In the below example 459 would be put in an array [4,5,9]
Now I want to take 4 add two zeros to the end because it has two numbers after it in the array
Then I want to take 5 and add one zero to it because there is one number after it in the array.
then 9 would have no zeros added to it because there are no numbers after it.
So final output would be 400,50,9
how can i best achieve this?
var num=459;
var nexint=num.toString().split("");
var finalint=nexint.map(Number);
var nextarr=[];
You need to use string's repeat method.
var num=459;
var a = (""+num).split('').map((c,i,a)=>c+"0".repeat(a.length-i-1))
console.log(a);
Here's another possible solution using a loop.
var num = 459;
var a = ("" + num).split('');
var ar = [];
for (var i = 0; i < a.length; i++) {
var str = a[i];
str += "0".repeat(a.length-i-1);
ar.push(str);
}
console.log(ar);
You could use Array#reduce and Array#map for the values multiplied by 10 and return a new array.
var num = 459,
result = [...num.toString()].reduce((r, a) => r.map(v => 10 * v).concat(+a), []);
console.log(result);
OP asked for a solution using loops in a comment above. Here's one approach with loops:
var num = 459
var numArray = num.toString().split('');
var position = numArray.length - 1;
var finalArray = [];
var i;
var j;
for(i = 0; i < numArray.length; i++) {
finalArray.push(numArray[i]);
for(j = 0; j < position; j++) {
finalArray.push(0);
}
position--;
}
console.log(finalArray);
The general flow
Loop over the original array, and on each pass:
Push the element to the final array
Then push X number of zeros to the final array. X is determined by
the element's position in the original array, so if the original
array has 3 elements, the first element should get 2 zeros after it.
This means X is the original array's length - 1 on the first pass.
Adjust the variable that's tracking the number of zeros to add before
making the next pass in the loop.
This is similar to #OccamsRazor's implementation, but with a slightly different API, and laid out for easier readability:
const toParts = n => String(n)
.split('')
.map((d, i, a) => d.padEnd(a.length - i, '0'))
.map(Number)
console.log(toParts(459))
I'm trying to implement a function which takes three arguments(min, max, step)and generates a range of integers from min to max, with the step. The first integer is the minimum value, the second is the maximum of the range and the third is the step.
Here is an example of what it should look like:
generateRange(2, 10, 2) should return array of [2,4,6,8,10].
I'm using the splice method to remove any existing elements in the array that are greater than the max argument.
function generateRange(min, max, step) {
var arr = [];
var count = min;
for (var i = 0; i < max / step; i++) {
arr[i] = count;
count = count + step;
arr[i] > max ? arr.splice(i, 1) : arr[i];
}
return arr;
}
console.log(generateRange(2, 10, 2));
Whenever I console.log my result I get a bunch of commas after the last item...so it looks like this: [2,4,6,8,10, , , , ]
It doesn't appear to be deleting the items. What am I missing? Thanks!
The ternary operator is a bit strange, as the expression is not stored. It fixes the array by removing too large values. That works once, but if there is a second time, i will have increased, and by the assignment to arr[i], the array's length is again as if there had been no splice performed before (except for the undefined value at that i-1 index).
It would be better to exit the loop before assigning a value that is outside of the range. There is no sense in continuing the loop in such a case.
So make the count variable your loop variable and condition:
function generateRange(min, max, step){
var arr = [];
for(var count = min; count <= max; count+=step){
arr.push(count);
}
return arr;
}
var res = generateRange(2, 10, 2);
console.log(res);
A less readable, but shorter ES6 version would be:
function generateRange(min, max, step){
return Array.from(Array(Math.floor((max-min)/step)+1), (x,i) => min+i*step);
}
let res = generateRange(2, 10, 2);
console.log(res);
JavaScript; I want to get the values in a string which always has the following format but also has an indeterminate number of variables:
Sample String "340=5;339=5;338=3;337=4;336=1;335=4;334=4;333=4"
The variables named as 340, 339, 338 etc. in this string store integer values such as 5, 4, 1 and so on. I want to extract the integer values for each of the variables (extracting what's after the equal signs) and store the sum and average value of them in two separate variables as "sum" and "avr".
Nothing fancy, hopefully nice and readable.
function calculate(myString) {
var values = myString.split(';');
var sum = 0;
values.forEach(function(el, i) {
sum += parseInt(el.split('=')[1]);
});
return {
sum: sum,
avg: sum / values.length
}
}
var results = calculate("340=5;339=5;338=3;337=4;336=1;335=4;334=4;333=4");
console.log(results.sum); // => 30
console.log(results.avg); // => 3.75
As you have a simple pattern, you can use a RegExp to find the bits of interest. Here is an example using String.prototype.split and Array.prototype.reduce
var str = "340=5;339=5;338=3;337=4;336=1;335=4;334=4;333=4";
var parts = (';'+str).split(/;\d+=/).slice(1), // passing the RegExp to .split
sum = parts.reduce(function(prev, cur) {
return prev + +cur; // notice String cur to Number
}, 0),
avr = sum / parts.length; // assuming you want mean average
console.log(sum, avr); // 30 3.75
I would split the string then loop through all the values to get the sum and average.
var str="340=5;339=5;338=3;337=4;336=1;335=4;334=4;333=4";
var array =str.split(/;?\d+=/g);
var sum=0;
for(i=0;i<array.length;i++){
if(!isNaN(parseInt(array[i])))
sum+=parseInt(array[i]);
else{
array.splice(i,1);
i--;
}
}
console.log(sum);
console.log(sum/array.length);
This is trivial string evaluation. The only method you need is split, and the only logic you need is a for loop.
var values = "340=5;339=5;338=3;337=4;336=1;335=4;334=4;333=4";
var sum = []
var avr = []
values = values.split(';');
for(i=0; i<values.length; i++){
numbers = values[i].split('=')
sum.push(numbers[0])
avr.push(numbers[1])
}
I'm trying to sum and then average a stream of data, some code here.
var getAverage = function(dataArray){
var total,
sample = dataArray.length,
eArray = Array.prototype.slice.call(dataArray);
for (var i = 0; i< sample; i++) {
total+= eArray[i];
}
return total;
}
var output = function(){
//source data
var dataArray = new Uint8Array(bufferLength);
analyser.getByteTimeDomainData(dataArray);
var average = getAverage(dataArray);
$('#average').text(average);
window.requestAnimationFrame(output);
Every element in the array returns a number, but it still returns NaN. Help?
Set total = 0; currently it is defaulting to undefined. undefined + a number = NaN, and NaN + a number = NaN.
The declared variable total is undefined which means it will create NaN (Not-a-Number) when a number is added to it.
Also, Typed Array (ArrayBuffer/views) and Array are not the same, and converting a typed array to an ordinary Array is making iteration slower as typed arrays are actual byte-buffers while Arrays are (node) lists. That on top of the cost of conversion itself.
Just add them straight forward. Remember to divide the sum on length and of course to initialize total:
var getAverage = function(dataArray){
var total = 0, // initialize to 0
i = 0, length = dataArray.length;
while(i < length) total += dataArray[i++]; // add all
return length ? total / length : 0; // divide (when length !== 0)
}
I've just started learning coding on code academy and I'm really new to this.
I'm trying to make this program ask the user for values which it adds to an array from which it calculates the sample standard deviation.
// This array stores the values needed
var figures;
getStandardDeviation = function() {
// I need at least two figures for a standard deviation
figures[0] = prompt("Enter a number:");
figures[1] = prompt("Enter a number:");
// Checks whether user wishes to add more values to the array
var confirm = prompt("Would you like to add another? (Y or N)").toUpperCase();
// I can't figure out why the following if statement is not executed
// It checks whether the user wishes to add more values and adds them to the array
// If not it breaks the for loop
if (confirm === "Y"){
for ( i = 0; i === 100; i++){
figures[i + 2] = prompt("Enter a number:");
confirm = prompt("Would you like to add another figure? (Y or N)").toUpperCase();
if (confirm === "N"){
break;
}
}
}
// The rest of the code works fine from here onwards
var sumx = 0;
var n = figures.length;
for(var i = 0 ; i < n ; i++) {
sumx += figures[i];
}
console.log("Sum = " + sumx);
var sumXsq = 0;
for( i = 0 ; i < n ; i++) {
sumXsq += (figures[i] * figures[i]);
}
console.log("Sum x squared = " + sumXsq);
var sxx = (sumXsq - (sumx * sumx)/n);
console.log("Sxx = " + sxx);
var v = sxx/(n - 1);
console.log("Variance = " + v);
var standardDev = Math.sqrt(v);
console.log("Standard Deviation = " + standardDev);
};
getStandardDeviation();
The program is supposed to ask me if I want to add more values to the array, then when I confirm, it gives me a prompt to add more values.
Currently, when I execute the program I input the numbers 56 and 67. The code then asks me if I wish to add more values, I then confirm this. Instead of letting me add more values it ignores this and calculates the standard deviation with the first two values (56 and 67).
The output is:
Sum = 05667
Sum x squared = 7625
Sxx = -16049819.5
Variance = -16049819.5
Standard Deviation = NaN
for ( i = 0; i === 100; i++){[...]} means
Set i to 0
If it's not true that i === 100 (that is: if i is not 100), end the loop
Do whatever I put inside the {} braces, once
Do i++
Back to 2
As the initial value for i is 0 and not 100, the code inside the loop is never executed. If you want it to go from 0 to 99, it should be for ( i = 0; i < 100; i++).
You don't actually need a for loop, though. A while loop would be better. A loop like while (true){[...]} would run until it hit a break statement. As you wouldn't have the i in that case, you could use figures.push(parseFloat(prompt("Enter a number:"))) instead (you should use parseFloat, as per what Vincent Hogendoorn said) . push adds a new value at the end of an array, so it's exactly what you need. Something like:
if (confirm === "Y"){
while (true){
figures.push(parseFloat(prompt("Enter a number:")));
confirm = prompt("Would you like to add another figure? (Y or N)").toUpperCase();
if (confirm === "N"){
break;
}
}
}
You could also change it so it doesn't ask if you want to stop if you don't have at least two values. That way you would be able to leave out that first part:
figures[0] = prompt("Enter a number:");
figures[1] = prompt("Enter a number:");
indeed your figures variable isn't defined as an array, like #James Donnely says.
Keep in mind you also fill in strings, so if you want to add up values you have to convert them to values.
you can use something like parseFloat for this.
if you don't use it, you sum up strings. 3+4 will be 34 instead of 7.
Your figures variable isn't defined as an array. Because of this figure[1] = prompt(...) never gets hit and a TypeError is thrown on var n = figures.length;.
Change:
var figures;
To:
var figures = [];
JSFiddle demo.
You can then replace the for loop you're using after if (confirm === "Y") with a recursive function:
// Push a user input number into the figures array
figures.push(prompt("Enter a number:"));
// Function to add a new number and ask if we want to add more
function addNewNumber() {
// Push a new user input number into the figures array
figures.push(prompt("Enter a number:"));
// Ask if the user wants to add another number
if (confirm("Do you want to add another number?"))
// If they do, call this function again
addNewNumber();
}
// Trigger the function for the first time
addNewNumber();
JSFiddle demo with recursion.
function StandardDeviation(numbersArr) {
//--CALCULATE AVAREGE--
var total = 0;
for(var key in numbersArr)
total += numbersArr[key];
var meanVal = total / numbersArr.length;
//--CALCULATE AVAREGE--
//--CALCULATE STANDARD DEVIATION--
var SDprep = 0;
for(var key in numbersArr)
SDprep += Math.pow((parseFloat(numbersArr[key]) - meanVal),2);
var SDresult = Math.sqrt(SDprep/numbersArr.length);
//--CALCULATE STANDARD DEVIATION--
alert(SDresult);
}
var numbersArr = [10, 11, 12, 13, 14];
StandardDeviation(numbersArr);