Learn Functional Programming by Building a Spreadsheet - Step 25

Hi there friendly cody peoples!
My assumption is that I’m not formulating correctrly the value that needs to be return if isEven(length) ? is true, but I’m at my witt’s ends as I can’t seem to figure out another way to formulate it.
Any handy suggestions? c:
Thanks a lot for your help in advance, happy coding!! :slight_smile:

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="./styles.css" />
    <title>Functional Programming Spreadsheet</title>
  </head>
  <body>
    <div id="container">
      <div></div>
    </div>
    <script src="./script.js"></script>
  </body>
</html>
/* file: styles.css */
#container {
  display: grid;
  grid-template-columns: 50px repeat(10, 200px);
  grid-template-rows: repeat(11, 30px);
}

.label {
  background-color: lightgray;
  text-align: center;
  vertical-align: middle;
  line-height: 30px;
}
/* file: script.js */
const isEven = num => num % 2 === 0;
const sum = nums => nums.reduce((acc, el) => acc + el, 0);
const average = nums => sum(nums) / nums.length;


// User Editable Region

const median = nums => {
  const sorted = nums.slice().sort((a, b) => a - b);
  const length = sorted.length;
  const middle = length / 2 - 1;
  isEven(length) ? average(middle) && average(middle) +1 : middle.value;
  return median
}

// User Editable Region


const range = (start, end) => Array(end - start + 1).fill(start).map((element, index) => element + index);
const charRange = (start, end) => range(start.charCodeAt(0), end.charCodeAt(0)).map(code => String.fromCharCode(code));

window.onload = () => {
  const container = document.getElementById("container");
  const createLabel = (name) => {
    const label = document.createElement("div");
    label.className = "label";
    label.textContent = name;
    container.appendChild(label);
  }
  const letters = charRange("A", "J");
  letters.forEach(createLabel);
  range(1, 99).forEach(number => {
    createLabel(number);
    letters.forEach(letter => {
      const input = document.createElement("input");
      input.type = "text";
      input.id = letter + number;
      input.ariaLabel = letter + number;
      container.appendChild(input);
    })
  })
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36

Challenge Information:

Learn Functional Programming by Building a Spreadsheet - Step 25

you need to assign this to something, the value is calculated and then it is not stored anywhere so it is lost

Hi! I’ve tried assigning it to a new const, using it as a return (taking off the later one), still doesn’t get admitted.
It’s asking me these steps:
1… You should return a value from the median function.
2. If LENGTH is even, you should return the average of the number at the MIDDLE index and the number after it.
3. 3. If LENGTH is odd, you should return the value at the MIDDLE index.

Hi there! Post your updated code here.

Hi! This is my updated code, sorry I didn’t think of posting it before

const median = nums => {
  const sorted = nums.slice().sort((a, b) => a - b);
  const length = sorted.length;
  const middle = length / 2 - 1;
  const medianEven = isEven(length) ? average[middle] && average[middle] +1 : middle.value;
  return medianEven
  
}

You aren’t using average() function to calculate the sorted array at middle index and after it.

Thank you for your help Hasan, this is where I’m at right now:

const median = nums => {
  const sorted = nums.slice().sort((a, b) => a - b);
  const length = sorted.length;
  const middle = length / 2 - 1;
   return isEven(length)
    ? average(sorted(middle) && sorted(middle + 1))
    : sorted[Math.floor(middle)];
  
}

I’m still not sure what the issue is

You can’t use the () round braces for an array index.

That along with using Math.ceil instead of Math.floor sorted it out, thanks a million, helpful as always

1 Like