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
β
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)
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
π οΈ 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)