简体   繁体   中英

Stuck on last two FreeCodeCamp Javascript Calculator tests on the test suite?

The two tests that aren't passing are as follows I'm having trouble figuring out how to implement this in code and pass them.

  1. When the decimal element is clicked, a "." should append to the currently displayed value; two "." in one number should not be accepted

  2. If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign.

I've used 3 callback functions to come this far and have a calculator in working order. Any ideas? Thanks in advance. Here is the codepen link:

https://codepen.io/nezmustafa123/pen/oNXwxmo The javscript code is here.

    //start with string
    var tempMem = ""; 

    const display = document.querySelector('#display');

    document.querySelectorAll('[data-value]').forEach(el => {
        el.onclick = e => {

            if(display.innerText === "0") {
                display.innerText = el.dataset.value
            } else  {
                display.innerText += el.dataset.value;
            }

        }


    })



    document.querySelector('#equals').onclick = () => {
            let result = eval(display.innerText);
            display.innerText = result;
    }

    document.querySelector('#clear').onclick = () => {
        display.innerText = 0;
    }

It might be too late but just it helps some-else. I am at the moment not yet conversant with JS DOM and therefore I will provide my following solution in simple JS code.

  1. When the decimal element is clicked, a "." should append to the currently displayed value; two "." in one number should not be accepted

use the following function to process the input to reduce multiple 0s or decimals to one 0 or one decimal

const reduceMultipleZerosAndDecimalsToOne = (input) => {
  const processedInput = [];
  for (let i = 0; i < input.length; i++) {
    const item = input[i];

    if ((item.match(/[.]/g) || []).length > 1) {
      // if item has multiple decimals between numbers 5.5.5
      if (item.match(/^[0-9]\.[0-9]\.[0-9]$/)) {
        const item2 = item.replace(/[.]/g, " ").trim().replace(/\s+/, "."); // replace multiple ... with one .
        const item3 = item2.replace(/\s+/, ""); // replace remaning whitespace
        // console.log(item3);
        if (item3) {
          processedInput.push(item3);
        }
      } else {
        //if item has multiple decimals between numbers eg 5...55
        // console.log(item);
        const item2 = item.replace(/[.]/g, " ").trim().replace(/\s+/, "."); // replace multiple ... with one .
        // console.log(item2);
        if (item2) {
          processedInput.push(item2);
        }
      }
    } else if (item.match(/^0+/g)) {
      const item2 = item.replace(/^0+/g, 0); // replace multiple 0ss before a number to one zero
      if (item2) {
        processedInput.push(Number(item2));
        //   console.log(item2);
      }
    } else if (Number(item) == 0) {
      const item2 = Number(item); // convert multiple 0s to one 0
      if (item2) {
        processedInput.push(item2);
      }
    } else {
      processedInput.push(item);
    }
  }
  return processedInput;
};
  1. If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign.

here too you could used a function to make input to be -input if preceded by - sign after the / or X or + or - operations. Note I have only tested the function on '5X-5' therefore adjust as appropriate

let regex1;
let regex2;
let unWanted;
let wanted;

// a function to make input to be -X if preceded by - sign after /X+-
const correctFormatNegativeNumbers = (input, clickedMethods) => {
  const regex1 = /[0-9],[\/|X|+|-],-,[0-9]/g; // test if input has negative number and is preceded with /X+-
  const regex2 = /^(-,[0-9],[\/|X|+|-],[0-9])/g; // test if input has negative number and is followed with /X+-
  const regex3 = /^(-,[0-9],[\/|X|+|-](.*?))/g; // test if input has a starting negative number and is followed with /X+- then anything
  const regex4 = /((.*?)[\/|X|+|-],-,[0-9](.*?))/g; // test if input has negative number that is preceded with anyhting and /X+- and is followed with /X+-

  if (regex3.test(input) || regex4.test(input)) {
    const unWanted1 = "-,";
    const wanted1 = "-";
    const unWanted2 = ",-,";
    const wanted2 = ",-";

    const input2 = input
      .slice()
      .toString()
      .replace(unWanted1, wanted1)
      .replace(unWanted2, wanted2);

    //drop - from methods
    const newMethods = input2
      .replace(/[0-9]|-[0-9]/g, "")
      .replace(/,-,/g, ",")
      .replace(/-,/g, "");

    const processedItems = [input2.split(","), newMethods];
    return processedItems;

    // change -, input to -input
  } else if (regex1.test(input)) {
    console.log("Regex is regex1");
    const unWanted = ",-,";
    const wanted = ",-";

    const input2 = input.slice().toString().replace(unWanted, wanted);
    console.log(input2);

    //drop - from methods
    const newMethods = input2
      .replace(/[0-9]|-[0-9]/g, "")
      .replace(/,-,/g, ",")
      .replace(/-,/g, "");

    const processedItems = [input2.toString().split(","), newMethods];
    return processedItems;

    // change -, input to -input
  } else if (regex2.test(input)) {
    console.log("Regex is regex2");
    const unWanted = "-,";
    const wanted = "-";

    const input2 = input.slice().toString().replace(unWanted, wanted);
    // console.log(input2);

    //drop - from methods
    const newMethods = input2
      .replace(/[0-9]|-[0-9]/g, "")
      .replace(/,-,/g, ",")
      .replace(/-,/g, "");
    // console.log(newMethods);

    const processedItems = [input2.split(","), newMethods];
    return processedItems;

    // change -, input to -input
  } else if (
    regex1.test(input) == false ||
    regex2.test(input) == false ||
    regex3.test(input) == false ||
    regex4.test(input) == false
  ) {
    console.log(input + " doesnt have regex");
    // console.log(input);
    const processedItems = [input.toString().split(","), clickedMethods];
    return processedItems;
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM