-3

what i am trying to do is to assign operations function to prompt command so i can type in two numbers a type of operation and get the result in html this is my code so far

function add(a, b) {
    return a + b;
};
function substract(a, b) {
    return a - b;
};
function multiply(a, b) {
    return a * b;
};
function divide(a, b) {
    return a / b;
};

function operations(a, b, typoOfOp) {
    Prom = typeOfOp(a,b)
    document.write(elProm)
    return Prom
}

operations = prompt("Write two numbers and a type of operation")
raziel
  • 1
  • If you assign to the variable `operations`, that replaces the function with the same name. – Barmar May 22 '21 at 00:27
  • `prompt()` returns a string. You can use `split()` to split it into an array using a delimiter. – Barmar May 22 '21 at 00:27
  • It would be easier if you just used 3 separate prompts. – Barmar May 22 '21 at 00:28
  • 2
    Do yourself a favor and learn how to use a simple form to do this – charlietfl May 22 '21 at 00:29
  • If the user types a string like `add`, you'll need to do something to find the function with that name. See https://stackoverflow.com/questions/5834318/are-variable-operators-possible – Barmar May 22 '21 at 00:29
  • 2
    Do yourself another favor: Forget that `document.write()` exists. See https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – Barmar May 22 '21 at 00:29
  • There are few mistakes, but the biggest is expecting `typoOfOp` to be a function typed by the user! Check the prompt reference here: https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt – Vijay Dev May 22 '21 at 02:50

1 Answers1

-1

I think it would be simpler and cleaner to write one function and use if/else statements to perform the calculations.

FikraOps
  • 12
  • 2