Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Is there any way to get an equation out of a string?

L0L2G00D

Coder
JavaScript:
let equation = prompt("equation?")
equation = "5+5"
equation = "1-3"
equation = "7/3"

any ideas? it all needs to be in the same string for the application I might use it for.
 
When I offer a choice of things I use radio buttons:
Code:
<form action="YOUR PAGE HERE">
    <p>Which Equation</p>
    <input type="radio" id="equation1" name="equation" value="Bike">
    <label for="equation1">5+5</label><br>

    <input type="radio" id="equation2" name="equation" value="Car">
    <label for="equation2">1-3</label><br>

    <input type="radio" id="equation3" name="equation" value="Boat">
    <label for="equation3">7/3</label><br><br>
    <input type="submit" value="Submit">
</form>
 
Hey there, @L0L2G00D.

I've used this page from MDN: https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt - more specifically, I scrolled down until I found "Notes", which gave information on how to handle integer values(the default value returned is a string, however the solution shown in the Notes section shows you how to cast the value to an integer).

I've assumed that you're looking for the user to be able to insert two separate numbers and then combine them together, which means that the equation variable, I decided to split into two separate variables.

JavaScript:
<!DOCTYPE html>
<html>
    <body>
        <!--...-->
    </body>
    <script>
        let equation1 = Number(prompt("equation left?", ""));
        let equation2 = Number(prompt("equation right", ""));

        console.log(equation1 + equation2)
    </script>
</html>

I haven't dealt with the operation itself though(+, -, *, /, etc.) as I believe you'll need to know how to handle strings for that - I'm not experienced in JavaScript, so I cannot continue.

I hope something like this is what you're looking for.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom