15,747,059 members
1.00/5 (1 vote)
See more:
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  ========================
calc(5,15,'+')
20 // <======  EXPECTED OUTPUT
calc(50,15,'-')
35 // <======  EXPECTED OUTPUT
calc(50,2,'*')
100 // <======  EXPECTED OUTPUT
calc(500,2,'/')
250 // <======  EXPECTED OUTPUT
calc(500,2)
'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.";
}
}```
Posted
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.

Solution 1

There is nothing in that code statement that is actually right!
JavaScript
`num1 === typeof(number)`
Should be
JavaScript
`"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:
JavaScript
`(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!