Skip to content

Commit dbfc87b

Browse files
committed
9th Function
1 parent b1b074d commit dbfc87b

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

functions.js

+55
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,61 @@ function square(num){
121121
* @return {number} the result
122122
*/
123123

124+
function calculate(str,num1,num2){
125+
126+
var result=0;
127+
128+
//find out what the operation is
129+
switch(str.toUpperCase()){
130+
131+
case 'ADD':
132+
str='+';
133+
result = num1+num2;
134+
console.log(num1+' '+str+' '+num2+' = '+result);
135+
break;
136+
137+
case 'SUBTRACT':
138+
str='-';
139+
result = num1-num2;
140+
console.log(num1+' '+str+' '+num2+' = '+result);
141+
break;
142+
143+
case 'MULTIPLY':
144+
str='*';
145+
result = num1*num2;
146+
console.log(num1+' '+str+' '+num2+' = '+result);
147+
break;
148+
149+
case 'DIVIDE':
150+
str='/';
151+
152+
//if the second number is not zero, go ahead and divide
153+
if(num2!==0){
154+
result = num1/num2;
155+
console.log(num1+' '+str+' '+num2+' = '+result);
156+
}
157+
158+
else{
159+
result = undefined;
160+
}
161+
162+
break;
163+
164+
default:
165+
166+
console.log("Somethign went wrong, please try function again");
167+
168+
169+
}
170+
171+
172+
173+
174+
return result;
175+
}
176+
177+
178+
124179
/**
125180
* Returns true if `a` is greater than `b`.
126181
* @param {number} a

0 commit comments

Comments
 (0)