Learn Functional Programming by Building a Spreadsheet - Step 25

Tell us what’s happening:

I am trying to call isEven()
function after return keyword and to also check if length is even using isEven () function using syntax ternary### 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;
return function isEven () {length % 2 === 0
        ? (sorted[middle] + sorted[middle + 1]) / 2
        : sorted[Math.ceil(middle)];
    }
}

// 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/122.0.0.0 Safari/537.36 Edg/122.0.0.0

Challenge Information:

Learn Functional Programming by Building a Spreadsheet - Step 25

that’s not how you call a function, you may need a quick review on how to call a function

you mind helping out?

what kind of help do you need?

The function keyword defines a function, but you have already defined the isEven function in line 1 of your code. Do you think you need to define it again here?

The isEven function you have already defined takes a number. So it seems like you would want to pass a number into it when you call it.

P.S. You have already defined an average function on line 3 of your code. I’m guessing that might come in handy here as well.

how to use ternary syntax to check if the length is even using isEven() function. i want to know how to use the ternary syntax

I’m confused. This is using ternary syntax. You seem to know how to create a ternary?

i did that, but the error message says to call isEven() function after return

This isn’t how you call a function though. You are returning a function definition here instead of " check[ing] if length is even using your isEven function".

i need help getting past this stage

Can you post your most recent code attempt in here so we can see what you are doing? Also, perhaps explain in a little more detail what you are having trouble with?

here is my code attempt

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.slice(middle, middle))
        : sorted[Math.floor(middle)];
}

i did this and the error message is still asking to pass an array in the average() function

The triple back ticks need to be on lines all by themselves.

This looks pretty close to me. What is the hint telling you?

Also: “you’ll need to round the middle value up”

Does floor round up? Or Down?

to pass an array to my average function

So slice definitely returns an array. I think the hint might not be as detailed as it could be.

sorted.slice(middle, middle)

How many elements do you expect to be in the returned array when using the same number for both the first and second argument? If you don’t know, you can easily test this yourself.

So I looked ahead at the solution and I’m just going to tell you that you shouldn’t use slice to create the array to pass into the average function. Just to be clear, you can use slice to create this array, but the tests aren’t expecting it and thus you won’t pass even if you do it correctly. Rather than use slice, you’ll want to create the array manually using square brackets.