Jay
Jay

Reputation: 13

Stuck On How To Make An Array Inside A Function

I'm looking to make a function that will return a calculated grade when a new array is created, and then a function to calculate the grade is run.

My end product should look something like this:

let arr1 = ['125 ', ' 135 ', ' 10 ', ' 90 ', ' 90 ', ' 90, 90, 90];

let arr2 = ['95', '115', '7', '90', '83', '85', '90', '90'];

calcFnlPctg_3(arr1) -> 90.00

calcFnlPctg_3(2arr) -> 76.91

My current .js file looks like this:

function calcFnlPctg_3(arr) {
  arr = ["mid", "final", "xc", "p1", "p2", "p3", "p4", "p5"];
  projects = ((+p1 + +p2 + +p3 + +p4 + +p5)*0.3)/500;
  tests = ((+mid + +final + +xc)*0.7)/300;

  finalPercentage = (projects + tests) * 100;
    return finalPercentage;
}

I know it's incorrect, I'm just not sure how to replace the variables that something like "arr1" could provide if I'm not positive what the User testing the function is going to name the array. Any pointers are greatly appreciated.

Upvotes: 1

Views: 53

Answers (3)

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

Converting to numbers and using destructure will simplify.

let arr1 = ["125 ", " 135 ", " 10 ", " 90 ", " 90 ", " 90", "90", "90"];

let arr2 = ["95", "115", "7", "90", "83", "85", "90", "90"];

function calcFnlPctg_3(arr) {
  const [mid, final, xc, p1, p2, p3, p4, p5] = arr.map(x => Number(x));
  projects = ((p1 + p2 + p3 + p4 + p5) * 0.3) / 500;
  tests = ((mid + final + xc) * 0.7) / 300;

  finalPercentage = (projects + tests) * 100;
  return finalPercentage;
}

console.log(calcFnlPctg_3(arr1));
console.log(calcFnlPctg_3(arr2));

Upvotes: 0

SKeney
SKeney

Reputation: 2301

Your array looks like this:

let arr1 = ['125 ', ' 135 ', ' 10 ', ' 90 ', ' 90 ', ' 90, 90, 90];

I'm assuming it should look like this if you are storing an array of strings:

let arr1 = ['125 ', ' 135 ', ' 10 ', ' 90 ', ' 90 ', ' 90 ', ' 90 ',  ' 90' ];

As long as you have all projects follow the pattern of prefix p and a number this should work for your purposes.

let arr1 = ['125 ', ' 135 ', ' 10 ', ' 90 ', ' 90 ', ' 90', '90', '90'];

let arr2 = ['95', '115', '7', '90', '83', '85', '90', '90'];

function calcFnlPctg_3(arr) {
  let typeArray = ["mid", "final", "xc", "p1", "p2", "p3", "p4", "p5"];
  let projectsArray = [];
  let testsArray = [];

  arr.forEach((grade, index) => {
    if(typeArray[index].substr(0, 1) === 'p') {
      projectsArray.push(parseInt(grade));
    } else {
      testsArray.push(parseInt(grade));
    }
  });

  let projectsSum = 0;
  let testsSum = 0;
  projectsArray.forEach((grade) => { 
    projectsSum += grade;
  })

  testsArray.forEach((grade) => { 
    testsSum += grade;
  })

  let projects = projectsSum*0.3/500;
  let tests = testsSum*.7/300;

  let finalPercentage = (projects + tests) * 100;
  return finalPercentage
}

console.log(calcFnlPctg_3(arr1));
console.log(calcFnlPctg_3(arr2));

Let me know if you have other questions. Happy to explain.

Upvotes: 0

ray
ray

Reputation: 27245

If you just need to compute the average, you can do:

const arr1 = [125, 135, 10, 90, 90, 90, 90, 90];
const arr2 = [95, 115, 7, 90, 83, 85, 90, 90];

// convenience function for summing the values
const sum = values => values.reduce((acc, v) => acc + v, 0);

// function to compute the average
const avg = values => sum(values) / values.length;

console.log(avg(arr1)); // 90
console.log(avg(arr2)); // 81.875

Have you considered using objects instead of arrays to capture the grades for each project and test for a given student?

// using a more structured representation
// of a student's grades:
const studentA = {
  projects: {
    p1: 55,
    p2: 70,
    p3: 33,
    p4: 99
  },
  tests: {
    mid: 99,
    final: 89
  }
};

// utility function for summing an array of values
const sum = values => values.reduce((acc, v) => acc + v, 0);

// how to weight the grades by type
const weights = {
  projects: 0.3,
  tests: 0.7,
}

// takes a student object like the one above
// and returns the computed grade
function finalGrade(student) {
  const projectPoints = sum(Object.values(student.projects)) * weights.projects / 500;
  const testPoints = sum(Object.values(student.tests)) * weights.tests / 300;
  
  return (projectPoints + testPoints) * 100;
}

console.log(finalGrade(studentA));

Or, another option, associating a type with the grade:

// array of grades, each with an associated type:
const studentGrades = [
  {
    name: 'p1', // name isn't actually needed for this example
    type: 'project',
    value: 55
  },
  {
    name: 'p2',
    type: 'project',
    value: 70
  },
  {
    name: 'midterm',
    type: 'test',
    value: 70
  },
  {
    name: 'final',
    type: 'test',
    value: 70
  },
];

// utility function for summing an array of values
const sum = values => values.reduce((acc, v) => acc + v, 0);

// how to weight the grades by type
const weights = {
  projects: 0.3,
  tests: 0.7,
}

// extracts grade values of a given type
const valuesForType = (grades, type) => grades.filter(g => g.type === type).map(g => g.value);

// takes a grades array like the one above
// and returns the computed grade
function finalGrade(grades) {
  const projectPoints = sum(valuesForType(grades, 'project')) * weights.projects / 500;
  const testPoints = sum(valuesForType(grades, 'test')) * weights.tests / 300;

  return (projectPoints + testPoints) * 100;
}

console.log(finalGrade(studentGrades));

Upvotes: 1

Related Questions