What Would Be The Output Of This JS Code (pics) - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › What Would Be The Output Of This JS Code (pics) (1898 Views)
| What Would Be The Output Of This JS Code (pics) by sixpathsofpain(op): 7:04pm On Jul 03, 2022 |
What would be the output of the code below
|
| Re: What Would Be The Output Of This JS Code (pics) by JoyousFurnitire(m): 7:09pm On Jul 03, 2022 |
>> since n is not equal to 1 >> return 3 * 2; >> 6 |
| Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain(op): 7:34pm On Jul 03, 2022 |
JoyousFurnitire:honestly bro please if you can just help me do a little explanation, I'd really appreciate because function declaration and recursions are confusing me seriously. I don't even know where you got 2 from to multiply it by 3. |
| Re: What Would Be The Output Of This JS Code (pics) by Nobody: 7:55pm On Jul 03, 2022 |
![]() |
| Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain(op): 8:06pm On Jul 03, 2022 |
GREATIGBOMAN:what's funny? ![]() |
| Re: What Would Be The Output Of This JS Code (pics) by Nobody: 8:07pm On Jul 03, 2022 |
sixpathsofpain:. |
| Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain(op): 8:09pm On Jul 03, 2022 |
GREATIGBOMAN:Ok |
| Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain(op): 8:41pm On Jul 03, 2022 |
JoyousFurnitire:and please bro, what's the use of the first if and return statement? because I don't see any need for them |
| Re: What Would Be The Output Of This JS Code (pics) by JoyousFurnitire(m): 9:03pm On Jul 03, 2022*. Modified: 9:42pm On Jul 03, 2022 |
sixpathsofpain:Ok, I'll advice you practice functions on freecodecamp. Do all the the JavaScript if you can. > The function 'ftl' takes an input 'n' > n is given as 3 from the console.log(ftl(3)); sixpathsofpain:> ^^^ So the function states if n is exactly equals to 1 (type and value denoted with (===), the function should return 1 (the if statement) > But unfortunately, the number is greater than 1 so the function will skip to the next statement. > Which is since n is not equal to 1, > Remember n is given as 3, so just replace n with 3. > So it will be:
> What's really happening is that the function is calling itself within it self, More like a pregnant mother with a featous of herself (if that makes sense, doesn't really represent recursion but that's one way to look at it) ![]() > Now function ftl(2) on the right when called will return 2 > Finally it will be
|
| Re: What Would Be The Output Of This JS Code (pics) by jbreezy: 9:07pm On Jul 03, 2022 |
sixpathsofpain:Your code will give a stack overflow error without the if statement. It is called the base case of a recursive function. |
| Re: What Would Be The Output Of This JS Code (pics) by JoyousFurnitire(m): 9:07pm On Jul 03, 2022 |
GREATIGBOMAN:You're a troll ![]() |
| Re: What Would Be The Output Of This JS Code (pics) by jbreezy: 9:10pm On Jul 03, 2022 |
JoyousFurnitire:Like God damn!!!...then @op, you were the one that dropped "variable chaining" stuff some days ago. Why not stick to the basics for now, before moving to the advanced. |
| Re: What Would Be The Output Of This JS Code (pics) by JoyousFurnitire(m): 9:38pm On Jul 03, 2022 |
jbreezy:Basics are important and freecodecamp covers all that, I don't think I Know advance stuffs and I'm good to go. I spent a lot of time with the basics. |
| Re: What Would Be The Output Of This JS Code (pics) by namikaze: 9:42pm On Jul 03, 2022 |
jbreezy:Recursion is actually basic, just like loops, especially in functional programming languages. |
| Re: What Would Be The Output Of This JS Code (pics) by namikaze: 9:48pm On Jul 03, 2022 |
sixpathsofpain:I'll assume you're trying to learn recursion, It may take a while before you get used to recursion, and if you've already learned about loops, it gets worse. You're best bet is a pen and paper or visual debugger to step through a recursive code. |
| Re: What Would Be The Output Of This JS Code (pics) by airsaylongcome: 9:51pm On Jul 03, 2022 |
Classical recursive solution for the Factorial function. JS programmers do you guys use === for equality? I had assumed that == was the standard for equality in modern Programming languages. That's aside from := which PASCAL used |
| Re: What Would Be The Output Of This JS Code (pics) by Nobody: 10:03pm On Jul 03, 2022 |
airsaylongcome:== in JavaScript is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values. As such. 2==2? true 2=="2" true 2==="2"? false 2===2? true |
| Re: What Would Be The Output Of This JS Code (pics) by Nobody: 10:10pm On Jul 03, 2022 |
| Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain(op): 10:35pm On Jul 03, 2022 |
Mehn this sh*t is disgusting |
| Re: What Would Be The Output Of This JS Code (pics) by tensazangetsu20(m): 10:40pm On Jul 03, 2022 |
airsaylongcome:=== Is used to also check variable types. == Is used for just equality. Something like 5 == '5' will return true but false for the triple equal |
| Re: What Would Be The Output Of This JS Code (pics) by JoyousFurnitire(m): 11:07pm On Jul 03, 2022 |
GREATIGBOMAN: ![]() |
| Re: What Would Be The Output Of This JS Code (pics) by jbreezy: 3:46am On Jul 04, 2022 |
namikaze:Op doesn't even have a solid knowledge of function yet. |
| Re: What Would Be The Output Of This JS Code (pics) by jbreezy: 3:48am On Jul 04, 2022 |
JoyousFurnitire:I wasn't referring to you though. You can see I @op |
| Re: What Would Be The Output Of This JS Code (pics) by JoyousFurnitire(m): 7:08am On Jul 04, 2022 |
jbreezy:I was only supporting your point though so the op can see it's impossible to skip the basics ![]() |
| Re: What Would Be The Output Of This JS Code (pics) by airsaylongcome: 8:09am On Jul 04, 2022 |
@Tensazangetsu20 and @GreatIgboman Thanks for the explanation. Now I hate all these loosely typed languages more. Why for God's sake will int=string ever be true on earth? |
| Re: What Would Be The Output Of This JS Code (pics) by tensazangetsu20(m): 8:12am On Jul 04, 2022 |
airsaylongcome:Java has it too and it's strongly typed. |
| Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain(op): 8:22am On Jul 04, 2022 |
JoyousFurnitire:bro thanks for your explanation. It really helped me alot. I'm beginning to rap my head around recursions little by little but I know this I going to take time mehn but I know I wouldn't give up no matter how long it takes, I can't just stop going back to try and figure it out Please I need your advice on something. I feel you're one of the few reasonable and matured one's on this thread.. Please the reason I wanted to learn JAVASCRIPT in the first place was because I want to become a full-stack web developer. And I just jumped in JavaScript straight on. after sometime and a few research, I found out I needed to learn HTML and CSS first before JavaScript and then react (I don't know what any of them does cause I'm a noob ) so now I'm planning on quitting JAVASCRIPT for now, to go focus on HTML first then CSS before continuing JAVASCRIPT and then what ever should follow (for me to become full-stack) would you advise me to quit JavaScript now and go focus on those (HTML and CSS) or I can continue with JAVASCRIPT before HTML and CSS?And please just help me give an outline of how my learning path should be to become full-stack ( because I don't like starting what I don't Finnish ) Thanks @tensazangetsu20 please you too can help me contribute |
| Re: What Would Be The Output Of This JS Code (pics) by tensazangetsu20(m): 8:29am On Jul 04, 2022 |
sixpathsofpain:https://www.nairaland.com/6829914/how-go-learning-programming-start Purely my own opinion but also make research and come up with what you want too |
| Re: What Would Be The Output Of This JS Code (pics) by airsaylongcome: 8:40am On Jul 04, 2022 |
tensazangetsu20:This is news to me! I really need to get to speed with all the new additions in these programming languages |
| Re: What Would Be The Output Of This JS Code (pics) by emetisuccess(m): 8:46am On Jul 04, 2022 |
The code up there is based on condition. If n equals 1, output 1 If n is not equals 1 Return n*func(n-1) Then u gave a value for n, which is 3. Anywhere that n is in the code, it will substitute it for 3. 3* func(3-1) =6 |
| Re: What Would Be The Output Of This JS Code (pics) by tensazangetsu20(m): 8:47am On Jul 04, 2022 |
airsaylongcome:Java even has var now that lets you write variables without declaring their types. It used to be just == and equals() |
| Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain(op): 8:53am On Jul 04, 2022 |
tensazangetsu20:Thanks |
As A Programmer, What Will Be The Output Of This Code? • I Asked Chatgpt To Write A Poem About Nairaland. Here's The Output • Wrote My First JS Code On VS Code.... Lol (pics) • 2 • 3 • 4
Fortran 77 (i Need Your Help) • Skilled Programmer Needed • Can You Create An Ai Chatbot That Can Speak Nigerian Pidgin?



) so now I'm planning on quitting JAVASCRIPT for now, to go focus on HTML first then CSS before continuing JAVASCRIPT and then what ever should follow (for me to become full-stack) would you advise me to quit JavaScript now and go focus on those (HTML and CSS) or I can continue with JAVASCRIPT before HTML and CSS?