JavaScript Challenge - Calculate the Mean and Mode, Calculate the Mean and Mode

let numbers = [500, 400, 400, 375, 300, 350, 325, 300]

//* Average

const getAverage = (numbers) => {
 let sum = 0
 numbers.forEach((element) => sum = sum + element ) 
 
 return(sum/numbers.length)
}

//* Mode

//build an object that has each number as a key and number of occurrences as a value 
//E.g. [500: 1, 400: 2, 375:1, 300: 2, 350: 1, 325:1]
//In this example there are two modes.  The requirement is to return the first mode.

const getMode = (numbers) => {

  const obj = {};
    
  numbers.forEach((number, index) => {//not obj[number] return true if a number does not exist as a key in the object
    
    if (!obj[number]) { // does not exists therefore add it
      obj[number] = 1 
      
    } else {
      obj[number] += 1 // does exist so increment the count
      
    }
  }) 
  
   
  //iterate over array of number-objects and return the first mode
  let currentHighValue = 0
  let currentHighKey = -Infinity
  
  for (let key in obj) {
    const value = obj[key];

    if (value >= currentHighValue ) { 
      currentHighValue = value;        
      currentHighKey = key;
    }
  }
  
  return Number(currentHighKey)

} 

const statsFinder = (numbers) => {

 const average =  getAverage(numbers)
 const mode = getMode(numbers)

 return [average, mode]
}

function statsFinder(arr = []) { let mean = arr.reduce((acc, curr) => acc + curr, 0) / arr.length; let modeObj = {}; arr.forEach(el => { if (modeObj[el] === undefined) { modeObj[el] = { num: el, count: 1 }; } else { modeObj[el].count++; } }); let modeNum; Object.keys(modeObj).forEach(el => { if (modeNum === undefined) { modeNum = modeObj[el]; return; } if (modeObj[el].count > modeNum.count) { modeNum = modeObj[el]; } else if (modeObj[el].count === modeNum.count) { if (arr.indexOf(modeObj[el].num) < arr.indexOf(modeNum.num)) { modeNum = modeObj[el]; } } }); let mode = modeNum.num; return [mean, mode]; } console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300])) // Leave this so we can test your code: module.exports = statsFinder;
function statsFinder(array) { // Write your code here let result // function to sum values of Array and duplicate values in Array const avgArray = (total, num) => total + num const dupArray = array.filter((item, index) => array.indexOf(item) !== index) // Calculate mean of the Array result = array.reduce(avgArray) / array.length return [result, dupArray[0]] } console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300])) // Leave this so we can test your code: module.exports = statsFinder;
function statsFinder(array) {
  const mean = array.reduce((acc,cur)=>acc+cur)/array.length
  let modes = []
  array.map((val,index)=>{
    let counts = 0;
    array.map((valTwo,indexTwo)=>{
      val == valTwo && counts ++ 
    });
    counts > 1 && modes.push(val);
  })
  return [mean,modes[0]]
}
console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;

function statsFinder(array) {
const mean = array.reduce((a,b)=> a+b,0)/array.length
const mode= […new Set(array)].map(el => [el, array.filter(x => x === el).length]).reduce((acc, cur) => acc[1] >= cur[1] ? acc : cur)[0]
return [mean, mode ]
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;

function statsFinder(array) { // Write your code here var storedArray = array; var sum = 0; array.forEach((num) => {sum += num}); var average = sum/array.length; const count = {}; array.forEach(function(e) { if(count[e] === undefined) { count[e] = 0 } count[e] += 1 }) var bestElement = 0; var bestCount = 0; Object.entries(count).forEach(([k,v]) => { if (v >= bestCount){ bestElement = k; bestCount = v; } }); var mode = parseInt(bestElement); return [average, mode]; } console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300])) // Leave this so we can test your code: module.exports = statsFinder;

JavaScript challenge: Calculate the Mean and Mode #get-help:javascript

function statsFinder(array) {
  // Write your code here

  let sum = 0;
  let mean = 0;
  let mode = [ ];

  for (let i = 0; i < array.length; i++) {
    sum += array[i];
  }
  mean = sum / array.length;

  const obj = {};
  array.forEach((key) => {
    if (!obj[key]) {
      obj[key] = 1;
    } else {
      obj[key] += 1;
    }
  })
  
  let highestValue = 0;
  let highestValueKey = 0;
  
  for (let key in obj) {
    let value = obj[key];
    if (value >= highestValue) {
      highestValue = value;
      highestValueKey = Number(key);
    }
  }
  mode = highestValueKey;
  return [mean, mode];
}


console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;