Learn Functional Programming by Building a Spreadsheet - Step 64

Tell us what’s happening:

Hi, to be honest ive completed all until ctep 64 and i have one problem, i dont understand almost nothing from the moment we started writing this:

const evalFormula = (x, cells) => {
  const idToText = id => cells.find(cell => cell.id === id).value;
  const rangeRegex = /([A-J])([1-9][0-9]?):([A-J])([1-9][0-9]?)/gi;
  const rangeFromString = (num1, num2) => range(parseInt(num1), parseInt(num2));
  const elemValue = num => character => idToText(character + num);
  const addCharacters = character1 => character2 => num => charRange(character1, character2).map(elemValue(num));
  const rangeExpanded = x.replace(rangeRegex, (_match, char1, num1, char2, num2) => rangeFromString(num1, num2).map(addCharacters(char1)(char2)));
  const cellRegex = /[A-J][1-9][0-9]?/gi;
  const cellExpanded = rangeExpanded.replace(cellRegex, match => idToText(match.toUpperCase()));
}

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 */

// User Editable Region



// User Editable Region


const isEven = num => num % 2 === 0;
const sum = nums => nums.reduce((acc, el) => acc + el, 0);
const average = nums => sum(nums) / nums.length;

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.ceil(middle)];
}

const spreadsheetFunctions = {
  sum,
  average,
  median
}

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));

const evalFormula = (x, cells) => {
  const idToText = id => cells.find(cell => cell.id === id).value;
  const rangeRegex = /([A-J])([1-9][0-9]?):([A-J])([1-9][0-9]?)/gi;
  const rangeFromString = (num1, num2) => range(parseInt(num1), parseInt(num2));
  const elemValue = num => character => idToText(character + num);
  const addCharacters = character1 => character2 => num => charRange(character1, character2).map(elemValue(num));
  const rangeExpanded = x.replace(rangeRegex, (_match, char1, num1, char2, num2) => rangeFromString(num1, num2).map(addCharacters(char1)(char2)));
  const cellRegex = /[A-J][1-9][0-9]?/gi;
  const cellExpanded = rangeExpanded.replace(cellRegex, match => idToText(match.toUpperCase()));
}

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;
      input.onchange = update;
      container.appendChild(input);
    })
  })
}

const update = event => {
  const element = event.target;
  const value = element.value.replace(/\s/g, "");
  if (!value.includes(element.id) && value.startsWith('=')) {

  }
}

Your browser information:

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

Challenge Information:

Learn Functional Programming by Building a Spreadsheet - Step 64

What are we searching for?Cells to make math with, or what? what is x and cells, why do i need to do idToText?The only thing i understand is regex.Am i so stupid or is it a task being really hard, like, i can keep making it step by step, but i dont understand what and why am i doing this.Why do we even need not used parameters in function?

Hi @artemkirpotenko

Most of the projects may not make any sense until you finish them.
It is kind of like working on an actual project in a team. Sometimes you will be given a task to do without any context of why you are doing it.

It looks like:

  1. evalFormula takes x a value entered into a cell in the spreadsheet, and cells is the cell of the spreadsheet where that value is entered.
  2. idToText finds the cell reference, such as cell B2, of the spreadsheet
  3. rangeRegex is used to check that a cell range is valid. For this spreadsheet it goes from cell A1 to J99
  4. rangeFromString converts the string values of the cell numbers into a range of integers
  5. elemValue takes a number and a letter to get a specific spreadsheet cell, such as B4

… and so on.

Take another look when you finish the project.

Happy coding

2 Likes

But i feel like it would be crazy hard for me to write this part by myself without instructions, is it okay for it to be like this?I find this part to be very hard for understanding and writing it by my own

It is perfectly normal to feel frustrated when learning to code. You are exposed to maths and computer science at the same time. Even people who are experts at spreadsheets may not fully understand what happens behind the scenes with formulas.

This is why the forum is here, to help fellow coders on their learning journey.

2 Likes

This is pretty normal and I wouldn’t worry about it.

Following a guide like this is kind of like reading someone else’s code, it’s kind of hard to understand fully at first because you didn’t conceive of it and design every single step.

Some things will click as you go along, and some things you might not get this time around. Maybe it will click in a later project.

Fully understanding a large project and how it all interacts can be difficult and take a lot of time. Especially when you are in the middle of it and it’s incomplete.

If you have specific questions, you can ask here!

1 Like

So after spending 2 hours with chatGPT im starting to understand it, i feel like i understand 30-40% of it, will try to make it clear as sky tomorrow, ill write closer to the end of the project if i have and question, thank you guys:)

1 Like

How are you able to determine if what ChatGPT says is accurate?

1 Like

Tough question, do you think it is mostly answering wrong?Because i was reading what it says and i just tried to explain to myself all of this code and now i understand everything, and seems like i start to get it until
const elemValue = num => character => idToText(character + num);
And now trying to understand what is num and character and where do i use it

Do you suggest not using it for an explanaition?

I would use it for very limited scope questions where you can easily verify or check another source.

Do you know exactly what AI/ML is and how it works? Have you ever seen an image generated with too many fingers or strange results? In an image we can easily identify the problem and keep prompting for a result that works. Hard to identify those problems when it’s programming topics that you’re not expert on.

Just know that it’s a sentence generator, not a source of absolute truth.

2 Likes

So i dont know, spent already 5 hours on this, i stuck here

 const elemValue = num => character => idToText(character + num);
  const addCharacters = character1 => character2 => num => charRange(character1, character2).map(elemValue(num));

i DONT understand, where do i get NUM and CHARACTER1, CHARACTER2 from,
i UNDERSTAND where do i get NUM1, NUM2, CHAR1, CHAR2 from, but not CHARACTER1, CHARACTER2 and NUM, im starting to get crazy mad, so i stop there, reaching for your help, because i dont understand ChatGPT’S explanaition at all, it says i get it in future, but i have no idea, where and what “captures” it, before, i understood, that regex captures all the arguments i will work with, but after it, i have no idea

so i guess char1, char2 are just for better understanding that i am going to have a range of numbers from num 1 to num 2, and num is just the first (number = num1) and so, rangeFromString makes an array of numbers, then for each number i make addCharacters, which creates an array of characters, then elemValue adds one by one number from rangeFromString and addCharacters, for example A + 1 = A1, then it gets its value by idToText, and so now i have an array of numbers like [10, 20, 30, 40, 50]

Write down any questions you have. When you have completed the practice project, go back over the questions and see if things start making sense.

If you want to get a better understanding right now, you could skip to the last step to see the nearly completed code.

2 Likes

One has to consider where the ChatGPT answer comes from when utilizing it as a resource to answer questions. How would you know without further research that the response on ChatGPT is the absolute correct answer to the question.
Meaning, does the answer found on ChatGPT not technically come from another human writing the answer for the ChatGPT and how would you know that the ones responsible for answering are the ‘absolute’ on the answer? It is best practice to work to find solutions on your own and use multiple research options to come to the best understanding and solution for yourself because we all comprehend and learn differently and it is better to determine what learning practice works best for you, and remember that repitiion allows you to gain knowledge and even though these steps can sometimes seem irrelevant, severely complex, and complicated, once you have worked through to the end you will have a better understanding of what the need for that confusing step may have been.
I wish you all the best with your coding and I can relate to the confusion you have found and with the utmost repsect I can offer you nothing more than experience with these as a reassurance that you will find what works best for you and go with what you feel allows you to learn best! I use all kinds of research and rely heavily on MDN reading and forumns of experienced coders to offer advice and options to learn better. I hope it gets easier for you to relate to in the future and hopefully these steps get easier for you as well. :slightly_smiling_face:

so i finished it, and i still dont inderstand where do we get arguments for function to work with from, and some of them in general.
For example:

const infixToFunction = {
  "+": (x, y) => x + y,
  "-": (x, y) => x - y,
  "*": (x, y) => x * y,
  "/": (x, y) => x / y,
}

const infixEval = (str, regex) => str.replace(regex, (_match, arg1, operator, arg2) => infixToFunction[operator](parseFloat(arg1), parseFloat(arg2)));

Where do i get arg1, operator, arg2 from?I guess from regex, but still i dont understand in which way he places each match?

const highPrecedence = str => {
  const regex = /([\d.]+)([*\/])([\d.]+)/;
  const str2 = infixEval(str, regex);
  return str === str2 ? str : highPrecedence(str2);
}

What this thing does, from what i understood its to process multiplying or dividing in first place, before + or -. but where does str come from?And why do i need str2?

const applyFunction = str => {
  const noHigh = highPrecedence(str);
  const infix = /([\d.]+)([+-])([\d.]+)/;
  const str2 = infixEval(noHigh, infix);
  const functionCall = /([a-z0-9]*)\(([0-9., ]*)\)(?!.*\()/i;
  const toNumberList = args => args.split(",").map(parseFloat);
  const apply = (fn, args) => spreadsheetFunctions[fn.toLowerCase()](toNumberList(args));
  return str2.replace(functionCall, (match, fn, args) => spreadsheetFunctions.hasOwnProperty(fn.toLowerCase()) ? apply(fn, args) : match);
}

Here i dont get why do we need to make a variable to call highPrecedence if its called noHigh which means im probably doing + or -, what is infix in str2?What is args in toNumberList?Where does it come from?What is fn?Args again?Because of me not really understanding infixEval i dont really understand what is going on with str2 trough the function.
I feel like im so messed up in all directions, mostly arguments, i dont know what to do

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.