-2

Please help me in JavaScript: The program that I am coding is one that takes in an expression in prefix notation and outputs the same expression in infix notation. The idea behind this program is as follows:

if the user enters 1 + 2 the expected output is + 1 2. All valid symbols are +, -, *, /, and %. The amount of numbers that the user can enter should be limitless (so for example, if I enter 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, the program should return + 1 2 3 4 5 6 7 8 9 10).

Could someone please help me fill in the comment out portion of the loop, and if you think there's a better approach to the problem entirely, I am open to that!

function infix(input) {
  var x = input.split(''); // splits each variable and stores it in an array
  var output = [];
  var final = " "; // will be used to store our infix expression
  for (var i = 0; i < x.length; i++) {
    //if x[i] is any of the following : "+, -, *, /, or %" , store it in array output at index 0
    //else if x[i] is a number : store it in an index of array output that is >= 1

  }
  for (var j = 0; j < output.length; j++) {
    var final = x[0] + x[j];
  }
  console.log(final);
}

infix("1 + 2 + 3")
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    What if I enter `1 + 2 - 3` ? – mplungjan Nov 21 '19 at 05:41
  • first go through what is infix and prefix and ask what you want exactly http://www.openbookproject.net/books/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html – harsha reddy Nov 21 '19 at 05:45
  • I created you a snippet and changed the `int` to `var` – mplungjan Nov 21 '19 at 05:45
  • [...new Set("1 + 2 - 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10".match(/[^\w\s]/gi))].join('')+ "1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 {}+ 9 + 10".replace(/[^\d\s]/gi, '').replace(/\s/g, '') – PraveenKumar S Nov 21 '19 at 05:52
  • `const string = "1 + 2 + 3"; console.log(string.match(/(\d+)/g)) console.log(string.replace(/\s+/g,"").match(/(\W)/g))` – mplungjan Nov 21 '19 at 05:53
  • Does this answer your question? [Converting Infix to prefix notation in JavaScript](https://stackoverflow.com/questions/59131763/converting-infix-to-prefix-notation-in-javascript) – customcommander Dec 13 '19 at 19:57
  • Make up your mind. Your title says 'converting from infix to prefix' but your question says the opposite. Which is it? – user207421 Apr 16 '23 at 03:57

1 Answers1

-1

Here's a snippet:

function infix(input){
  const specialCharacters = ['+', '-', '*', '/', '%'];
  const allCharacters = input.split('');

  const prefixes = [];
  const numbers = [];
  
  // go through all chars of input 
  for (let i = 0; i < allCharacters.length; i++) {
    const thisCharacter = allCharacters[i];

    // If the char is contained within the list of 'special chars', add it to list of prefixes.
    if (specialCharacters.includes(thisCharacter))
        prefixes.push(thisCharacter);

    // In case this is a whit space, just do nothing and skip to next iteration
    else if (thisCharacter === ' ') 
      continue;

    // If it's a number, just add it to the array of numbers
    else 
      numbers.push(thisCharacter);
  }
  
  // Merge both arrays
  const final = [...prefixes, ...numbers];

  // Back to string
  const finalString = final.join(' '); 

  console.log(final);
  console.log('String format: ' + finalString);
}

infix('1 + 2 - 3');

Notice:

  1. I replaced var by the new ES6 specification, const and let. (Use const always, use let if you have to re-write)
  2. I am not sure if you want to keep all the symbols if you have them, so I made an array. If you want only one symbol, instead of keeping an array, just keep a single variable
  3. Add an extra case for white spaces
Bruno Smaldone
  • 208
  • 3
  • 11