DEV Community

Precious Afolabi
Precious Afolabi

Posted on

Weeks 4 & 5: Recursion and Arrays in JavaScript

Two Weeks of Core JavaScript Concepts

Weeks 4 and 5 covered two fundamental topics: recursion and arrays. Here's what I learned and built.

Week 4: Recursion and Completing the Calculator
Started Week 4 by finishing my CLI calculator from Week 3. University tests pushed it back, so I adjusted my timeline. The calculator includes basic operations, advanced operations like power and square root, memory functions, calculation history, and chain operations using a prev keyword.
javascriptCalculator Ready

add 5 3
Result: 8
multiply prev 2
Result: 16
store
Stored: 16

Then moved into recursion. Reading about it was confusing, and watching videos was still abstract. Building simple examples made it click. Understanding how each function call builds on the previous one, then returns values back up.
Recursion Examples
Factorial:
javascriptfunction factorial(n) {
if (n === 1) return 1;
return n * factorial(n - 1);
}
Fibonacci:
javascriptfunction fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
String Reversal:
javascriptfunction reverseString(str) {
if (str === "") return "";
return reverseString(str.substring(1)) + str.charAt(0);
}
Palindrome Checker:
javascriptfunction isPalindrome(str) {
if (str.length <= 1) return true;
if (str[0] !== str[str.length - 1]) return false;
return isPalindrome(str.substring(1, str.length - 1));
}

Built recursive solutions for factorials, Fibonacci sequences, string reversal, and palindrome checking. Each one reinforced the pattern: base case to stop recursion, recursive case that calls itself with a simpler input.
Week 5: Arrays and Method Chaining
Week 5 was about arrays, from basic operations to chaining methods that changed how I approach data manipulation.
Array Fundamentals
Started with creating arrays, accessing elements, and basic manipulation with push, pop, shift, and unshift.
The critical lesson: arrays are reference types.
javascriptlet arr1 = [1, 2, 3];
let arr2 = arr1; // Reference copy
let arr3 = [...arr1]; // Shallow copy

arr2.push(4);
console.log(arr1); // [1, 2, 3, 4]
console.log(arr3); // [1, 2, 3]
When you assign arr2 = arr1, both variables point to the same array. The spread operator creates a new array.

Iteration Methods
Explored different ways to loop through arrays: traditional for loops, for...of loops, and forEach. Ran performance tests on arrays with a million elements. Traditional for loops won on speed, forEach wins on readability.
Transform Methods
map() transforms every element:
javascriptconst numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);
// [2, 4, 6, 8, 10]
filter() selects elements that pass a test:
javascriptconst numbers = [1, 2, 3, 4, 5, 6];
const evens = numbers.filter(num => num % 2 === 0);
// [2, 4, 6]
reduce() accumulates to a single value:
javascriptconst numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, num) => acc + num, 0);
// 15

Search and Test Methods
find() and findIndex() locate elements:
javascriptconst users = [{id: 1, name: "Alice"}, {id: 2, name: "Bob"}];
const user = users.find(u => u.id === 2);
some() and every() test conditions:
javascriptconst numbers = [1, 2, 3, 4, 5];
numbers.some(n => n > 3); // true
numbers.every(n => n > 0); // true

Method Chaining
This is where things clicked. Instead of nested loops, chain operations:
javascriptconst numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Sum of squares of even numbers
const result = numbers
.filter(n => n % 2 === 0)
.map(n => n * n)
.reduce((sum, n) => sum + n, 0);
// 220
Each step is clear: filter for even numbers, square them, sum the results.

Key Lessons

Recursion: Every recursive function needs a base case and a recursive case. The base case stops the recursion, the recursive case solves a simpler version of the problem.
Arrays as references: Be conscious of how you copy arrays. Assignment creates a reference, spread operator creates a shallow copy.

Array methods: Map for transforming, filter for selecting, reduce for accumulating. These cover most data transformations.
Method chaining: Chain operations to create readable data pipelines instead of nested loops.

Moving Forward
From here, I'm going into advanced array manipulation and data structures.

Question: What concept took you multiple attempts to understand?

Top comments (0)