Click here to Skip to main content
15,063,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have to write a function that takes 3 arguments the first 2 are numbers and the third is an arithmetic operator, so it is either + , -, *, / and it executes the appropriate operation according to the provided arithmetic operator. I have to make sure to test the function with all 4 arithmetic operations should the operator be missing, the function should return 'wrong data provided'. For example:

//======================  EXAMPLE  ========================
20 // <======  EXPECTED OUTPUT
35 // <======  EXPECTED OUTPUT
100 // <======  EXPECTED OUTPUT
250 // <======  EXPECTED OUTPUT
'wrong data provided' // <======  EXPECTED OUTPUT

I writing this function but, it just returns undefined:

function calc(num1, num2, sign){
    if (num1 === typeof (number) && num2 === typeof (number) && sign === "+" || "-" || "*" || "/" ){
        return parseInt.num1, parseInt.sign, parseInt.num2;
    } else {
        return "Wrong data provided.";

I know that after creating the variables I have to check the data type, but I don't know how to do it properly, or, I know the function is wrong and I don't know how to doit properly. If you could help me would be great, thinks!

What I have tried:

<pre>function calc(num1, num2, sign){
    if (num1 === typeof (number) && num2 === typeof (number) && sign === "+" || "-" || "*" || "/" ){
        return parseInt.num1, parseInt.sign, parseInt.num2;
    } else {
        return "Wrong data provided.";
Updated 18-Aug-21 19:49pm
Richard MacCutchan 19-Aug-21 4:43am
At the risk of boring you to death, stop trying to write code that you really do not understand. I gave you the link to the Javascript tutorials, so you need to go and work through them slowly and carefully. Then work through them again. And repeat that process until you fully understand the language and its syntax. There are no shortcuts to learning, only practice, practice, practice.
gabriel19971029 19-Aug-21 4:48am
Yes, thanks, I'll keep doing that. W3School Mozilla MDN and this are my main 3 friends.

1 solution

There is nothing in that code statement that is actually right!
num1 === typeof(number)
Should be
"number" === typeof(num1)
And the same for num2
The operator test: you can't write it like that: "-" is not a condition, nor are "*" or "/"
You would need to compare each item separately:
(sign === "+" || sign === "-" ... ){

You then identify that sign is a operator, but try to assume it's an integer by parsing it.
Then instead of working out the operation, you try to return three values - which Javascript won't let you do ...

THrow the whole lot away, sit down and think about what you are trying to do: that looks like you skimmed your lesson, skimmed the homework question, ignored what you should know about Javascript and threw some random junk down in the hope that we would do the work for you ...
gabriel19971029 19-Aug-21 4:41am
I'm sorry if that how it looks like, but I ain't got much in my theory book related to this exercise,I haven't even finish my first week in programming.
OriginalGriff 19-Aug-21 5:00am
If you've been given the exercise, it's because you have been given enough in the preceding text / lesson(s) to do it, if you understand the material you have been taught.
Read it again, and think about what you are being asked. It isn't complicated, not really - it just looks like it is because you aren't sure what is going on yet.
gabriel19971029 19-Aug-21 4:42am
Anyways, thanks a lot for the help I really appreciate.
OriginalGriff 19-Aug-21 4:57am
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900