Inserting operators into an array of numbers

I’m trying to add operators into an array of numbers.

Just wondering if you have tips on solving this and if possible how I can conceptualise this effectively, because I’ve tried quite a bit and I’m still struggling. My current code is…

function insertOperators(charArray, numArray) {
    let operatorRegex = /\+|\-|\//g
    let operatorCount = 0;
    let specialOperator;
    for(let i = charArray.length - 1; i>0; i--) {
        if (charArray[i].match(operatorRegex)
        || charArray[i] ==="*"
        && charArray[i-1] !=="*"
        && charArray[i+1] !=="*") {

            charArray = ["2", "-", "1", "+", "3"]
            numArray = ["2", "1", "3"]

            specialOperator = charArray.slice(i, i+1);
            console.log(operatorCount + i);
            console.log(charArray[i], 'match at', i);
            numArray.splice(operatorCount + i, 0, ...specialOperator);
        // handle exponents
        if (charArray[i] === "*"
        && charArray[i-1] === "*") {
            console.log('still recorded');
            specialOperator = charArray.slice(i-1, i+1);
            joined = specialOperator.join('')
            let exponentPosition = i - 1;
            numArray.splice(exponentPosition, 0, joined);

            console.log('match at', i);
    return numArray;


// insertOperators([“1”, “1”, “+”, “6”], [“11”, “6”]); :white_check_mark:
// insertOperators([“1”,“2”, “-”, “3”], [“12”, “3”]); :white_check_mark:
// insertOperators([“6”,“0”, “/”, “3”], [“60”, “3”]); :white_check_mark:
// insertOperators([“5”,“1”, “", “2”], [“51”, “2”]); :white_check_mark:
// insertOperators([“2”, “*”, “*”, “3”], [“2”, “3”]) :white_check_mark:
// insertOperators([“2”, “*”, “*”, “3”, “+”, “5”], [“2”, “3”, “5”]) :white_check_mark:
insertOperators([“2”, “-”, “1”, “+”, “3”],[“2”, “1”, “3”])
// insertOperators([“2”, "
”, “*”, “3”, “+”, “5”, “-”, “2”], [“2”, “3”, “5”, “2”])

Can you add some more information about this function and its goals and the algorithm you are attempting to complete?

Some missing info,
What are the input params supposed to be? I see two arrays but I am not sure why there are two or how they are meant to interact with each other.

What is the output supposed to look like given these two arrays?

In your own words, what is your algorithm for solving this?

The formatter doesn’t like * characters and I’ve just noticed how this could be confusing.

I’m trying to make a string equation parser. i.e something that can evaluate"1+11+4-2". Currently I’m splitting the string into a charArray.

Then I’m using regex to create an array of numbers from the string.

// Given a numberArray
["11", "6"]

// and a charArray

find any char that isn’t a number in charArray and insert it into the numberArray.

I have a mathKey object which holds basic arithmetic methods which can be used later once the array has been correctly reassembled.

i.e [“11”, “+”, “6”]
or [“1”, “+”, “11”, “+”, “4”, “-”, “2”]

Hope that is somewhat coherent. Happy to give more information.

1 Like

one question more for me, if the goal is simply to evaluate the equation, why not just do that by extracting the numbers and performing the arithmetic as you go?
(ie. why go to the trouble of handling 2 arrays for this?)

1 Like

That is a good question. The parser needs to be able to take care of parenthesis and ** which makes this a bit trickier than a left to right job.

I am able to look for the parenthesis’ and slice the nested elements of it into a seperate array.

I ran into some issues with the * character and the exponent operator which the second if statement attempts to handle.

At this point I’ve slightly lost the plot, and it’s probably best to take a break. Still I’d appreciate any more insight you have and I’ll have a think on whether I’m over complicating it :thought_balloon:

1 Like

ah yes of course. Makes sense.
I’m trying to remember the arithmetic order of operations rules here.
They’re supposed to be ** first? (or brackets first?)

1 Like

Parenthesis first… Please excuse my dear Aunt Sue!!

I created something that takes a charArray like yours and combines the numbers together and the double asterisk together based on the code you wrote so far.

Take a look and see if it gives you some answers.
(it is super basic but it works for the stated purpose of having the charArray turn into the output you needed) Beyond that though, not sure if it will help your ultimate goal.

1 Like

Thank you so much! I’m going to have a good read through this over the next two days and try to understand our approach.

I’m so grateful for your help :blush:

1 Like

make sure you fork it as I occasionally go and delete these temporary codepens

I have saved it locally, thanks for the heads up :slight_smile:

1 Like

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