DAY 2 .DSA challange for 45 days
March 16,2024
begin with simple and straightforward problems to test my concepts.
solution:
class Solution { getTable(N) { //code here let bag=[] for(let i=1;i<=10;i++){ bag.push(i*N) } return bag } }
solution:
class Solution {
reverseEqn(s) {
//code here
s=s.match(/[0-9]+|[\/\*\+\-]/g)
return s.reverse().join("")
}
}
solution:
while (num >= 10) { let sum = 0; while (num > 0) { sum += num % 10; num = Math.floor(num / 10); } num = sum; } return num;};
OR
var addDigits = function(num) { num=num.toString()
while(num.length>1){ let temp=num.split("").map(Number) const sum = temp.reduce((accumulator, currentValue) => accumulator + currentValue, 0); num=sum.toString() } return +num
};
solution:
class Solution {
isDigitSumPalindrome(n){
//code here
n=n.toString()
let sum=0
for(let i=0;i<n.length;i++){
sum+=Number(n[i])
}
sum=sum.toString()
for(let i=0;i<sum.length;i++){
if(sum[i]!=sum[sum.length-1-i]){
return 0
}
}
return 1
}
}
Today, I started learning recursion by starting with very basic concepts
to get the hang of it.
1.power function
let n = 2;
let r = 4
function fun(n, r) {
if (r == 0) {
return 1;
} else {
return n * fun(n, r - 1);
}
};
console.log(fun(n, r));
2.Binary Equivalent-Recursion
// let n=15
let n=128
let bag=[]
function binary(n){
if(n==0){
return bag.reverse()
}
else {
bag.push(n%2)
return binary(Math.floor(n/2))
}
}
console.log(binary(n).join(" "))

Comments
Post a Comment