DEV Community

Laxman Nemane
Laxman Nemane

Posted on

🧠 DSA Day 6

Today was a productive day! I worked on a few fundamental JavaScript concepts that are super useful in Data Structures and Algorithms:

βœ… Count Digits Program
Wrote a function to count the number of digits in a number. Practiced using loops and type conversion for this.
Example:

function countDigits(num) {
  return num.toString().length;
}
console.log(countDigits(12345)); // Output: 5
Enter fullscreen mode Exit fullscreen mode

βœ… Math Methods – Explored:

Math.floor(4.7) ➝ 4 (rounds down)

Math.ceil(4.2) ➝ 5 (rounds up)

Math.round(4.5) ➝ 5 (rounds to nearest)

Math.abs(-10) ➝ 10 (returns absolute value)
Enter fullscreen mode Exit fullscreen mode

These methods are very handy when working with calculations, arrays, or loops.

βœ… Palindrome Number
Wrote a function to check if a number reads the same backward (like 121, 1331).
Example:

function isPalindrome(num) {
  let str = num.toString();
  return str === str.split('').reverse().join('');
}
console.log(isPalindrome(121)); // Output: true
console.log(isPalindrome(123)); // Output: false
Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ All these basics are helping me get more comfortable with JavaScript and strengthen my logic-building skills for DSA.

πŸ” Small wins each day add up. On to Day 7!

Top comments (0)