Javascript Module 9
Javascript Module 9
1. Reverse a String
Question: Write a function to reverse a given string.
Code :
function reverseString(str) {
return str.split("").reverse().join("");
}
2. Check Palindrome
Question: Write a function to determine if a string is a palindrome (reads the same forwards
and backwards).
Code :
function isPalindrome(str) {
const reversed = str.split("").reverse().join("");
return str === reversed;
}
3. Count Vowels
Question: Write a function to count the number of vowels in a string.
Code :
function countVowels(str) {
const vowels = 'aeiou';
let count = 0;
for (let char of str.toLowerCase()) {
if (vowels.includes(char)) {
count++;
}
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
}
return count;
}
4. Capitalize Words
Question: Write a function to capitalize the first letter of each word in a sentence.
Code :
function capitalizeWords(sentence) {
return sentence.split(" ").map(word => word.charAt(0).toUpperCase()
+ word.slice(1)).join(" ");
}
5. Check Anagrams
Question: Write a function to check if two strings are anagrams of each other.
Code :
Code :
function longestWord(sentence) {
const words = sentence.split(" ");
let longest = words[0];
for (let word of words) {
if (word.length > longest.length) {
longest = word;
}
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
}
return longest;
}
7. Remove Duplicates
Question: Write a function to remove duplicate characters from a string.
Code :
function removeDuplicates(str) {
let result = "";
for (let char of str) {
if (result.indexOf(char) === -1) {
result += char;
}
}
return result;
}
8. Count Characters
Question: Write a function to count the occurrences of each character in a string.
Code:
function countCharacters(str) {
const charCount = {};
for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
}
return charCount;
}
9. Replace Spaces
Question: Write a function to replace all spaces in a string with '%20'.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Code :
function replaceSpaces(str) {
return str.replace(/\s/g, '%20');
}
Code :
function titleCase(sentence) {
return sentence.toLowerCase().replace(/(^|\s)\w/g, (match) =>
match.toUpperCase());
}
Code :
function hasSubstring(str, substr) {
return str.includes(substr);
}
Code :
function removeWhitespace(str) {
return str.replace(/\s/g, "");
}
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Code :
function isSubsequence(sub, str) {
let subIndex = 0;
for (let char of str) {
if (char === sub[subIndex]) {
subIndex++;
}
if (subIndex === sub.length) {
return true;
}
}
return false;
}
Code :
function firstNonRepeatingChar(str) {
const charCount = {};
for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
}
for (let char of str) {
if (charCount[char] === 1) {
return char;
}
}
return null;
}
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Code :
function isRotation(str1, str2) {
if (str1.length !== str2.length) {
return false;
}
const combinedStr = str1 + str1;
return combinedStr.includes(str2);
}
Code :
function countWords(sentence) {
return sentence.split(" ").filter(word => word !== "").length;
}
// Example usage:
const numbers = [1, 2, 3, 4, 5];
console.log(sumArray(numbers)); // Output: 15
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Code :
function averageArray(arr) {
const sum = sumArray(arr);
return sum / arr.length;
}
function sumArray(arr) {
let sum = 0;
for (let num of arr) {
sum += num;
}
return sum;
}
const numbers = [1, 2, 3, 4, 5];
// Example usage:
console.log(averageArray(numbers)); // Output: 3
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
}
}
return true;
}
function secondLargest(arr){
let secondLargest =arr[0];
let largest = arr[0];
for(let element of arr){
if(largest< element){
secondLargest = largest;
largest = element
} else if(secondLargest < element && element!=largest){
secondLargest = element
}
}
return secondLargest
}
// Example usage
console.log(secondLargest(arr))
Code :
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
1
2 3
4 5 6
7 8 9 10
Code :
// you can chhanges the input as per as your input
let rows = 4;
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
}
console.log(pattern);
Code :
// you can change the size of row
let rows = 5;
Code :
// you can change the input by re-sizing the rows value
let rows = 5;
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
pattern += "\n";
}
console.log(pattern);
Code :
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
console.log(pattern);
Code :
// modify value as per the requirement
let rows = 5;
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]