7 lines of code turned into 57 lines!
Hi everyone,
Learning JavaScript is full of a roller-coaster ride, today I will be sharing my first best experience with js code.
To give you a back story, I just got a project that asked me to print the first 10 Fibonacci sequences,
I saw the code, got confused, and did understand anything then I thought why don't start from scratch,
I asked myself,
what is the Fibonacci number?
Where are they used?
Why do we use them?
I turned on my friend ChatGPT and got a deep understanding of it!
so here's what I talked to myself about while writing 7 lines of code,
Surprisingly it took me 57 lines of # comments to get it complete
take a look at it!
// how much numbers do you want?
// I want first 10 numbers of fibonacci sequence
// done! first initialize, how many numbers you want,
// ok
iWant = 10;
// good! now do you know what is fibonacci sequence?
// sorry, i dont know? what is it? and how it works?
// also where do we use it?
// ok, so
// what is fibonacci sequence?
// The Fibonacci sequence is a series of numbers where each number is the sum of the two numbers before it
// examples are 0,1,1,3,5,8
// ok! but what is real life examples of it?
// good! so Fibonacci sequence in followed by
// 1)Leaves on a stem
// 2)Leaves on a stem
// 3)Branching patterns of trees and rivers
// and many more!
// wow! nature is cool, but why it follows this sequence?
// great question,
// one word answer-> because it is an efficient way for growth and packing.
// Please explain me in some detail!
// ok here we go!
// 1) the arrangement of leaves on a stem following the Fibonacci sequence allows for maximum exposure to sunlight and air.
// 2)the spiral pattern of pineapples following the Fibonacci sequence allows for efficient packing of seeds or segments.
// In other words, nature uses the Fibonacci sequence because it is an efficient way to organize growth and maximize resources.
// ok! good now you understand what and why, ok
// so lets move with our code,
// first let's set our first 2 values;
let n1 = 0;
let n2 = 1;
// also let's declare the variable nextTerm; without initializing it,
let nextTerm;
// now lets loop them till we get first 10;
for(let i = 1; i <= iWant; i++){
// let's print our n1, remember we have set it to "0"
console.log(n1) /* output will be "0" */
// now we have our first value;
// now we want to print our next- term in sequence,
// lets do it by adding "n1" + "n2"
// now you know why? yes or no?
// yes because Fibonacci sequence is --->
// The Fibonacci sequence is a series of numbers where each number is the sum of the two numbers before it
// good!
nextTerm = n1 + n2;
// now our output will be 0 + 1 => 1
// now let's update the values of n1 and n2 so we could get next results!
//but how and why?
// so we know, Fibonacci is sum of 2 numbers before it, so its change them to get new results,
// so our n1 will change to n2 and n2 will change to nextTerm,
n1 = n2;
n2 = nextTerm;
// and yes press run button, we did it!
}
And yes, now I can write this code even in my sleep!
Understanding anything from scratch takes time seems silly, but trust me
Once you understand the basics your perspective will change.
This was my 1st real blog.
Thanks for reading bye!