I found the starting point to begin learning DSA. Go through these concepts once before jumping into DSA.
1. Programming Basics
✅ Functions
Functions are blocks of reusable code.
// Function to add two numbers
function add(a, b) {
return a + b;
}
console.log(add(3, 5)); // Output: 8
You call a function with arguments, and it returns a value.
✅ If-Else
Conditional statements to make decisions.
function isEven(n) {
if (n % 2 === 0) {
return "Even";
} else {
return "Odd";
}
}
console.log(isEven(4)); // Output: Even
console.log(isEven(7)); // Output: Odd
✅ Loops
Loops help you repeat tasks.
// Print numbers from 1 to 5
for (let i = 1; i <= 5; i++) {
console.log(i);
}
2. Practice Problems
⭐ Star Patterns (Basic Logic & Loops)
Pattern 1:
*
**
***
****
function printRightTriangle(n) {
for (let i = 1; i <= n; i++) {
let stars = "";
for (let j = 1; j <= i; j++) {
stars += "*";
}
console.log(stars);
}
}
printRightTriangle(4);
Pattern 2: Inverted Triangle
****
***
**
*
function printInvertedTriangle(n) {
for (let i = n; i >= 1; i--) {
let stars = "";
for (let j = 1; j <= i; j++) {
stars += "*";
}
console.log(stars);
}
}
printInvertedTriangle(4);
🔄 Palindromes
A number or word that reads the same forwards and backwards.
function isPalindrome(str) {
let reversed = str.split("").reverse().join("");
return str === reversed;
}
console.log(isPalindrome("madam")); // true
console.log(isPalindrome("hello")); // false
🔢 Digit Counting
function countDigits(n) {
let count = 0;
while (n !== 0) {
n = Math.floor(n / 10);
count++;
}
return count;
}
console.log(countDigits(12345)); // Output: 5
🔁 Reverse Integers
function reverseInteger(n) {
let rev = 0;
while (n !== 0) {
rev = rev * 10 + (n % 10);
n = Math.floor(n / 10);
}
return rev;
}
console.log(reverseInteger(1234)); // Output: 4321
🥈 Second Largest in Array
function secondLargest(arr) {
let first = -Infinity, second = -Infinity;
for (let num of arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num !== first) {
second = num;
}
}
return second;
}
console.log(secondLargest([5, 3, 1, 7, 6])); // Output: 6
🔁 Nested Loops (2D Logic)
// Multiplication table (1-3)
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
console.log(`${i} x ${j} = ${i * j}`);
}
}
More Good Problems To Consider:
✅ 1. Sum of Digits
Add all the digits of a number.
📌 Example: 123 → 1 + 2 + 3 = 6
function sumOfDigits(n) {
let sum = 0;
while (n !== 0) {
sum += n % 10; // get the last digit
n = Math.floor(n / 10); // remove last digit
}
return sum;
}
console.log(sumOfDigits(1234)); // Output: 10
✅ 2. Check if a Number is Prime
A number is prime if it has only two factors: 1 and itself.
📌 Example: 2, 3, 5, 7 are prime; 4, 6 are not.
function isPrime(n) {
if (n <= 1) return false;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) return false;
}
return true;
}
console.log(isPrime(7)); // true
console.log(isPrime(9)); // false
✅ 3. Fibonacci Series
A series where next number is the sum of previous two.
📌 Example: 0 1 1 2 3 5 8 13 ...
function printFibonacci(n) {
let a = 0, b = 1;
console.log(a); // first
if (n > 1) console.log(b); // second
for (let i = 2; i < n; i++) {
let next = a + b;
console.log(next);
a = b;
b = next;
}
}
printFibonacci(7); // Output: 0 1 1 2 3 5 8
✅ 4. Factorial of a Number
Multiply all natural numbers up to
n
.
📌 Example: 5! = 5×4×3×2×1 = 120
function factorial(n) {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
console.log(factorial(5)); // Output: 120
✅ 5. Armstrong Number
For 3-digit:
abc
→a³ + b³ + c³ = abc
📌 Example: 153 → 1³ + 5³ + 3³ = 153
function isArmstrong(n) {
let sum = 0;
let temp = n;
const digits = n.toString().length;
while (temp > 0) {
let digit = temp % 10;
sum += Math.pow(digit, digits);
temp = Math.floor(temp / 10);
}
return sum === n;
}
console.log(isArmstrong(153)); // true
console.log(isArmstrong(370)); // true
console.log(isArmstrong(123)); // false
✅ 6. LCM and GCD
📌 GCD (Greatest Common Divisor): largest number dividing both
📌 LCM (Least Common Multiple): smallest common multiple
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
function lcm(a, b) {
return (a * b) / gcd(a, b);
}
console.log("GCD:", gcd(12, 18)); // Output: 6
console.log("LCM:", lcm(12, 18)); // Output: 36
✅ 7. Count Vowels in a String
Vowels = a, e, i, o, u
function countVowels(str) {
let count = 0;
const vowels = "aeiou";
for (let char of str.toLowerCase()) {
if (vowels.includes(char)) {
count++;
}
}
return count;
}
console.log(countVowels("JavaScript")); // Output: 3
✅ 8. Find Maximum and Minimum in Array
function findMinMax(arr) {
let min = arr[0];
let max = arr[0];
for (let num of arr) {
if (num < min) min = num;
if (num > max) max = num;
}
return { min, max };
}
console.log(findMinMax([5, 2, 9, 1, 7]));
// Output: { min: 1, max: 9 }
After going through these concepts and problems, you will become confident in the basics of programming and be ready to start learning DSA.
Thanks for reading!
— Ranjan