+ 7
Somebody please help me identify What's wrong with this code?
25 Respuestas
+ 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())
+ 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.
+ 4
You're doing good then! Keep it up!
+ 3
Made more sense in this case
+ 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.
+ 3
About 15 years
+ 3
Thanks
+ 2
Thanks so much
+ 2
Hey guess what that code only lacked return statements 😂
https://code.sololearn.com/WxCjvEYBO3Pl/?ref=app
+ 2
I'm a novice really
+ 2
If I may ask how long have you been coding?
+ 2
I've been coding for barely a month 😁
+ 1
How do I tell if to return either sum or product?
+ 1
I think I'm stuck kindly correct it for me I'm freaking confused right now😂😂
+ 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());
+ 1
I've noticed you opted for swtch statements instead of if statements is there special reason?
+ 1
True if statements are a bit tricky in this situation
+ 1
I've put them and it works now
Wow I'm elated😁
+ 1
Wow😲
+ 1
Hi Sander