-3

I failed to find out reason of the output: 337

function mlt(a, b){
    if(!(a&&b)) {
        return 0;
    }else if(b==1){
        return a;
    } else {
        return (a + mlt(a, b-1));
    }
}
document.write(mlt(3, 11));
document.write(mlt(7, 1));
David
  • 208,112
  • 36
  • 198
  • 279
  • 4
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Aug 25 '22 at 15:57
  • Also, the output isn't `337` it's `33` then `7`, but the way you're outputting it is misleading and displays the two results together. – DBS Aug 25 '22 at 15:59
  • 337 is the result of 2 outputs placed together, not just one since you have `document.write(mlt(3, 11));` and `document.write(mlt(7, 1));` – Chris G Aug 25 '22 at 15:59
  • Use `console.log()` instead of `document.write()` then you'll see them on separate lines. – Barmar Aug 25 '22 at 16:01

1 Answers1

0

You're getting 337 simply because the results of your two function calls are 33 and 7 respectively. Since you're writing this with document.write its simply appending the 7 to the end of the 33 resulting in 337

document.write(mlt(3, 11)); returns 33 document.write(mlt(7, 1)); returns 7

AlignSD
  • 21
  • 1
  • 7