DEV Community

Thiru Sakthi
Thiru Sakthi

Posted on

Day 5 - Strings & Arrays Basics

Today I focused on strengthening core fundamentals using simple problems and for-loop logic.

🔁 Reverse a String
let val = "thiru";
let rev = "";

for(let i = val.length - 1; i >= 0; i--){
    rev += val[i];
}
console.log(rev);


🔍 Palindrome Check
function isPalindrome(val){
    let rev = "";

    for(let i = val.length - 1; i >= 0; i--){
        rev += val[i];
    }

    for(let i = 0; i < val.length; i++){
        if(val[i] !== rev[i]){
            return false;
        }
    }
    return true;
}
console.log(isPalindrome("val"));


🔤 Count Vowels & Consonants
function countVowels(str){
    const vowels = new Set("aeiou");
    let countV = 0;
    let countC = 0;

    for(let i = 0; i < str.length; i++){
        let ch = str[i].toLowerCase();

        if(ch >= "a" && ch <= "z"){
            if(vowels.has(ch)){
                countV++;
            } else {
                countC++;
            }
        }
    }
    return { vowels: countV, consonants: countC };
}

console.log(countVowels("aaa"));


🔄 Array  String
function arrToStr(arr){
    let newStr = "";

    for(let i = 0; i < arr.length; i++){
        newStr += arr[i];
    }

    return newStr;
}

console.log(arrToStr(["a", "a", "a"]));


🔄 String  Array
function strToArr(str){
    let newArr = [];

    for(let i = 0; i < str.length; i++){
        newArr.push(str[i]);
    }

    return newArr;
}

console.log(strToArr("aaa"));


🚫 Remove Duplicates
function removeDup(str){
    let result = "";

    for(let char of str){
        if(!result.includes(char)){
            result += char;
        }
    }

    return result;
}

console.log(removeDup("EDDA"));


🔁 Replace Characters
let value = "aaad";
let sValue = "a";
let rValue = "e";

let result = "";

for(let i = 0; i < value.length; i++){
    if(value[i] === sValue){
        result += rValue;
    } else {
        result += value[i];
    }
}

console.log(result);
Enter fullscreen mode Exit fullscreen mode

🧠 What I Learned

  • Traversing strings and arrays using loops
  • Building results step by step
  • Handling edge cases (like non-letter characters)
  • Writing clean and readable logic

Top comments (0)