Learn Functional Programming By Building a Spreadsheet - Step 73

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

const highPrecedence = str => {
 const regex = /(\d)(\*\/)(\d)/g;
}

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('=')) {

  }
}

In your highPrecedence function, declare a regex variable. Assign it a regular expression that matches a number (including decimal numbers) followed by a * or / operator followed by another number.

Each number, and the operator, should be in separate capture groups.

Why is my regex isn’t passing the test? Where is the fault?

Look at it here: https://regex101.com/

Would your regex capture decimal numbers (as opposed to integers?)

Yes, the regex would capture any numbers, including decimal numbers.

hey bro, have you found the solution yet? I’m currently stuck here too, let me know if any of you solved it

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Really, it does? Test it

/([\d]+[.][\d]+)([*]|[/])([\d]+[.][\d]+)/g;
Still can’t solve. Here is what I did:

  • Capture group 1 including 3 groups a digit with charcter class of one or more. second is a group of period, and the third is another group with digit .

  • Capture group 2 including 2 groups, the first for * or the second is / .

-Capture group 3 same of the Capture group 1 .

What else shoud I dom bearing in mind that I tested the pattern on Regex101.com and it match.

Kindly comment.
Thanks

Hm, I tested it and it didn’t match. Can I see your test?

I would start off more simple first, then maybe tackle decimals?

It does seem to work for decimals, although I had to fix this error first


a regular expression that matches a number (including decimal numbers) followed by a * or / operator followed by another number.

It needs to work for non-decimal numbers as well

1 Like

pkdvalis,
Thanks for your efforts.

  • Matter of fact , when I submitted my post it was free of errores, once I clicked Reply the backslash disappeared, I don’t know why !

  • I know that challange must be solved typically as it’s on our dear freecodecamp platform, although it could be solved in several ways.

Now I’m still stucked to pass the challange.
Thank you once again for your help.

Now it works for numbers and decimal after adding (or) (|).
Still not passing.

Another solution:
/([\d]+[\.]?[\d]+)([\*]|[\/])([\d]+[\.]?[\d]+)/g.

Ah, you need to enclose the regex in triple backticks (shares a key with tilde ~) using the code button in this editor. I’ve modified it for you. That’s why the escape character was disappearing.

Again, this works for numbers with decimals, but not integers. You need to make the decimal part optional.

Thank you, I already made the decimal part optional and, your comment and test is perfect which I amended the code of the first digit to be 0 or more instead of 1 or more. please check the attachments.



Can anyone give another solution, so that I pass the challange?

Please open a new thread and post your code.

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

solution removed

Adding some extra text.

    • ** solution redacted** **** * *…

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.