25 Answers
New Answerhttps://code.sololearn.com/WxCjvEYBO3Pl/?ref=app
3/4/2021 9:54:10 AM
Global Universe25 Answers
New AnswerYou 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())
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.
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.
Hey guess what that code only lacked return statements π https://code.sololearn.com/WxCjvEYBO3Pl/?ref=app
I think I'm stuck kindly correct it for me I'm freaking confused right nowππ
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());
I've noticed you opted for swtch statements instead of if statements is there special reason?
SoloLearn Inc.
4 Embarcadero Center, Suite 1455Send us a message