+ 7

Somebody please help me identify What's wrong with this code?

https://code.sololearn.com/WxCjvEYBO3Pl/?ref=app

4th Mar 2021, 9:54 AM
Global Universe
Global Universe - avatar
25 odpowiedzi
+ 7
You need to return values from your which() function function which() { if( b == "add" ){ return sum(p) ; } else if ( b == "multiply") { return mult(p); } else { return "sorry i cant do that"; } } console.log(which())
4th Mar 2021, 10:05 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
You already have that set up, what do you mean? If 'add' is entered at the 2nd prompt then b == "add" is true and the sum(p) function will run. Same for mult. If something else is entered then it will output the else.
4th Mar 2021, 10:11 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
You're doing good then! Keep it up!
4th Mar 2021, 12:04 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Made more sense in this case
4th Mar 2021, 11:21 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Global Universe That's what I was trying to tell you in my first post. You should change the else statement to return the string instead of calling console.log() on it, and you should also parseInt(p) before sending it to the functions, so it is doing an implicit conversion.
4th Mar 2021, 11:46 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
About 15 years
4th Mar 2021, 12:02 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Thanks
4th Mar 2021, 12:04 PM
Global Universe
Global Universe - avatar
+ 2
Thanks so much
4th Mar 2021, 11:16 AM
Global Universe
Global Universe - avatar
+ 2
Hey guess what that code only lacked return statements 😂 https://code.sololearn.com/WxCjvEYBO3Pl/?ref=app
4th Mar 2021, 11:31 AM
Global Universe
Global Universe - avatar
+ 2
I'm a novice really
4th Mar 2021, 11:59 AM
Global Universe
Global Universe - avatar
+ 2
If I may ask how long have you been coding?
4th Mar 2021, 12:01 PM
Global Universe
Global Universe - avatar
+ 2
I've been coding for barely a month 😁
4th Mar 2021, 12:03 PM
Global Universe
Global Universe - avatar
+ 1
How do I tell if to return either sum or product?
4th Mar 2021, 10:07 AM
Global Universe
Global Universe - avatar
+ 1
I think I'm stuck kindly correct it for me I'm freaking confused right now😂😂
4th Mar 2021, 10:13 AM
Global Universe
Global Universe - avatar
+ 1
let num = parseInt(prompt("Enter a positive number")); let op = prompt("Enter add or multiply"); function sum(n) { //Gauss formula for sum of 1-n return (n+1)*n/2; } /*function mult(n) { let product = 1; for(let i = 1; i <= n; i++) { product *= i; } return product; }*/ function mult(n) { if (n == 1) return 1; else return n * mult(n-1); } function which() { switch (op) { case "add": return "sum: " + sum(num); break; case "multiply": return "product: " + mult(num); break; default: return "Invalid option"; } } console.log(which());
4th Mar 2021, 11:13 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
I've noticed you opted for swtch statements instead of if statements is there special reason?
4th Mar 2021, 11:17 AM
Global Universe
Global Universe - avatar
+ 1
True if statements are a bit tricky in this situation
4th Mar 2021, 11:22 AM
Global Universe
Global Universe - avatar
+ 1
I've put them and it works now Wow I'm elated😁
4th Mar 2021, 11:32 AM
Global Universe
Global Universe - avatar
+ 1
Wow😲
4th Mar 2021, 12:03 PM
Global Universe
Global Universe - avatar
+ 1
Hi Sander
4th Mar 2021, 12:14 PM
Global Universe
Global Universe - avatar