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")
Here's a snippet:
Notice: