Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,158,305 members, 7,836,329 topics. Date: Wednesday, 22 May 2024 at 04:59 AM

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) (1260 Views)

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)

(1) (2) (Reply) (Go Down)

What Would Be The Output Of This JS Code (pics) by sixpathsofpain: 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: 7:34pm On Jul 03, 2022
JoyousFurnitire:
>> since n is not equal to 1
>> return 3 * 2;
>> 6
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
grin

3 Likes

Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain: 8:06pm On Jul 03, 2022
GREATIGBOMAN:
grin
what's funny? undecided
Re: What Would Be The Output Of This JS Code (pics) by Nobody: 8:07pm On Jul 03, 2022
sixpathsofpain:
what's funny? undecided

.
Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain: 8:09pm On Jul 03, 2022
GREATIGBOMAN:


Ur foolishness
Ok
Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain: 8:41pm On Jul 03, 2022
JoyousFurnitire:
>> since n is not equal to 1
>> return 3 * 2;
>> 6
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
sixpathsofpain:
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.

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:
and please bro, what's the use of the first if and return statement? because I don't see any need for them

> ^^^ So the function states if n is exactly equals to 1 (type and value denoted with (===), the function should return 1 (the if statement)



if (n === 1) {
return 1;
}


> 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,



return n * ftl(n - 1);


> Remember n is given as 3, so just replace n with 3.

> So it will be:



return 3 * ftl(3 - 1);




return 3 * ftl(2);


> 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) undecided

> Now function ftl(2) on the right when called will return 2



return 3 * 2;


> Finally it will be


6

1 Like

Re: What Would Be The Output Of This JS Code (pics) by jbreezy: 9:07pm On Jul 03, 2022
sixpathsofpain:
and please bro, what's the use of the first if and return statement? because I don't see any need for them
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:

Ur foolishness

You're a troll undecided
Re: What Would Be The Output Of This JS Code (pics) by jbreezy: 9:10pm On Jul 03, 2022
JoyousFurnitire:


You're a troll undecided
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.

2 Likes

Re: What Would Be The Output Of This JS Code (pics) by JoyousFurnitire(m): 9:38pm On Jul 03, 2022
jbreezy:
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.

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:
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.
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:
What would be the output of the code below
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:
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

== 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

2 Likes

Re: What Would Be The Output Of This JS Code (pics) by Nobody: 10:10pm On Jul 03, 2022
JoyousFurnitire:


You're a troll undecided
with love cheesy
Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain: 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:
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

=== 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

2 Likes

Re: What Would Be The Output Of This JS Code (pics) by JoyousFurnitire(m): 11:07pm On Jul 03, 2022
GREATIGBOMAN:

with love cheesy

grin
Re: What Would Be The Output Of This JS Code (pics) by jbreezy: 3:46am On Jul 04, 2022
namikaze:

Recursion is actually basic, just like loops, especially in functional programming languages.
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:


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.
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 wasn't referring to you though. You can see I @op

I was only supporting your point though so the op can see it's impossible to skip the basics undecided

1 Like

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?

1 Like

Re: What Would Be The Output Of This JS Code (pics) by tensazangetsu20(m): 8:12am On Jul 04, 2022
airsaylongcome:
@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?

Java has it too and it's strongly typed.
Re: What Would Be The Output Of This JS Code (pics) by sixpathsofpain: 8:22am On Jul 04, 2022
JoyousFurnitire:


I was only supporting your point though so the op can see it's impossible to skip the basics undecided
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 grin
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 smiley ) 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 smiley )
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:
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 grin
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 smiley ) 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 smiley )
Thanks

@tensazangetsu20
please you too can help me contribute

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

1 Like

Re: What Would Be The Output Of This JS Code (pics) by airsaylongcome: 8:40am On Jul 04, 2022
tensazangetsu20:


Java has it too and it's strongly typed.

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:


This is news to me! I really need to get to speed with all the new additions in these programming languages

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: 8:53am On Jul 04, 2022
tensazangetsu20:


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
Thanks

(1) (2) (Reply)

Please Help Me Write The Code To Connect My Vb Application to Ms Access. Please / How Can I Save Page On Operamini Android / Go Lang

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 45
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.