0% found this document useful (0 votes)
60 views

Array Objects-1

The document contains 15 questions about writing JavaScript functions to perform string operations and manipulation tasks. For each question, it provides the function description, suggested file name, test data and the code implementation of the function. The questions cover common string tasks like checking if a string is empty, splitting a string into an array, extracting characters, parameterizing strings, changing case, concatenating strings, inserting strings and counting/searching substrings.

Uploaded by

vijaymuttevi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Array Objects-1

The document contains 15 questions about writing JavaScript functions to perform string operations and manipulation tasks. For each question, it provides the function description, suggested file name, test data and the code implementation of the function. The questions cover common string tasks like checking if a string is empty, splitting a string into an array, extracting characters, parameterizing strings, changing case, concatenating strings, inserting strings and counting/searching substrings.

Uploaded by

vijaymuttevi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

// Q1. Write a JavaScript function to check whether a string is blank or not?

// File name suggestion: isBlank.js


// Test Data :
// console.log(isBlank('')); // true
// console.log(isBlank('abc')); // false
const isBlank = input => !input.length;
console.log(isBlank('')); // true
console.log(isBlank('abc')); // false

// Q2. Write a JavaScript function to split a string (sentence) and convert it into an
array of words?
// File name suggestion: stringToArray.js
// Test Data :
// console.log(stringToArray("Robin Singh")); // ["Robin", "Singh"]
const stringToArray = str => str.split(' ');
console.log(stringToArray('Robin Singh')); // ["Robin", "Singh"]

// Q3. Write a JavaScript function to extract a specified number of characters from a


string?
// File name suggestion: truncateString.js
// Test Data :
// console.log(truncateString("Robin Singh", 4)); // "Robi"
const truncateString = (str, length) => str.substr(0, length);
console.log(truncateString('Robin Singh', 4)); // "Robi"

// Q4. Write a JavaScript function to parameterize a string?


// File name suggestion: stringParameterize.js
// Test Data :
// console.log(stringParameterize("Robin Singh from USA.")); // "robin-singh-from-
usa"
const stringParameterize = str => str.toLowerCase().replace(/\s/g, '-');
console.log(stringParameterize('Robin Singh from USA.')); // "robin-singh-from-usa"

// Q5. Write a JavaScript function to capitalize the first letter of a string?


// File name suggestion: capitalize.js
// Test Data :
// console.log(capitalize('js string exercises')); // "Js string exercises"
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
console.log(capitalize('js string exercises')); // "Js string exercises"

// Q6. Write a JavaScript function to capitalize the first letter of each word in a string?
// File name suggestion: capitalizeWords.js
// Test Data :
// console.log(capitalizeWords('js string exercises')); // "Js String Exercises"
const capitalizeWords = str =>
str
.split(' ')
.map(word => `${word.charAt(0).toUpperCase()}${word.slice(1)}`)
.join(' ');
console.log(capitalizeWords('js string exercises')); // "Js String Exercises"
// Q7. Write a JavaScript function that takes a string which has lower and upper case
letters as a parameter and converts upper case letters to lower case, and lower case
letters to upper case?
// File name suggestion: swapCase.js
// Test Data :
// console.log(swapCase('AaBbc')); // "aAbBC"
const swapCase = str => {
let result = '';
for (let i = 0; i < str.length; i++) {
if (str[i] === str[i].toUpperCase()) {
result += str[i].toLowerCase();
} else {
result += str[i].toUpperCase();
}
}
return result;
};
console.log(swapCase('AaBbc')); // "aAbBC"

// Q8. Write a JavaScript function that takes a string which can have both lower and
upper case letters as a parameter and converts alternate character to upper case &
lower case, starting from upper case?
// File name suggestion: alternateCase.js
// Test Data :
// console.log(alternateCase('samsung')); // "SaMsUnG"
const alternateCase = str => {
let result = '';
for (let i = 0; i < str.length; i++) {
if (i % 2 === 0) {
result += str[i].toUpperCase();
} else {
result += str[i].toLowerCase();
}
}
return result;
};
console.log(alternateCase('samsung')); // "SaMsUnG"

// Q9. Write a JavaScript function to concatenates a given string n times (default is


1)?
// File name suggestion: repeat.js
// Test Data :
// console.log(repeat('Ha!')); // "Ha!"
// console.log(repeat('Ha!',2)); // "Ha!Ha!"
// console.log(repeat('Ha!',3)); // "Ha!Ha!Ha!"
const repeat = (str, len) => {
let result = str;
if (len && len > 1) {
for (let i = 1; i < len; i++) {
result += str;
}
}
return result;
};
console.log(repeat('Ha!')); // "Ha!"
console.log(repeat('Ha!', 2)); // "Ha!Ha!"
console.log(repeat('Ha!', 3)); // "Ha!Ha!Ha!"

// Q10. Write a JavaScript function to insert a string within a string at a particular


position (default is 1)?
// File name suggestion: insert.js
// Test Data :
// console.log(insert('We are doing some exercises.')); // "We are doing some
exercises."
// console.log(insert('We are doing some exercises.','JavaScript ')); // "JavaScript We
are doing some exercises."
// console.log(insert('We are doing some exercises.','JavaScript ',18)); // "We are
doing some JavaScript exercises."
const insert = (str, ins_str, n) => {
if (!n) {
n = 0;
}
if (!ins_str) {
ins_str = '';
}

return `${str.slice(0, n)}${ins_str}${str.slice(n)}`;


};
console.log(insert('We are doing some exercises.')); // "We are doing some
exercises."
console.log(insert('We are doing some exercises.', 'JavaScript ')); // "JavaScript We
are doing some exercises."
console.log(insert('We are doing some exercises.', 'JavaScript ', 18)); // "We are
doing some JavaScript exercises."

// Q11. Write a JavaScript function to chop a string into chunks of a given length?
// File name suggestion: stringChop.js
// Test Data :
// console.log(stringChop('w3resource')); // ["w3resource"]
// console.log(stringChop('w3resource',2)); // ["w3", "re", "so", "ur", "ce"]
// console.log(stringChop('w3resource',3)); // ["w3r", "eso", "urc", "e"]
const stringChop = (str, size = 1) => {
const result = [];
let smallStr = '';
for (let char of str) {
if (smallStr.length === size) {
result.push(smallStr);
smallStr = '';
}
smallStr += char;
}
if (smallStr.length) {
result.push(smallStr);
}
return result;
};
console.log(stringChop('w3resource')); // ["w3resource"]
console.log(stringChop('w3resource', 2)); // ["w3", "re", "so", "ur", "ce"]
console.log(stringChop('w3resource', 3)); // ["w3r", "eso", "urc", "e"]

// Q12. Write a JavaScript function to count the occurrence of a substring in a string?


// File name suggestion: count.js
// Test Data :
// console.log(count("The quick brown fox jumps over the lazy dog", 'the')); // 2
const count = (text, word) => {
let count = 0,
index;
text = text.toLowerCase();
word = word.toLowerCase();
while (index !== -1) {
index = text.indexOf(word, index);
if (index !== -1) {
count++;
index++;
}
}
return count;
};
console.log(count('The quick brown fox jumps over the lazy dog', 'the')); // 2

// Q13. Write a JavaScript function to find a word within a string?


// File name suggestion: searchWord.js
// Test Data :
// console.log(searchWord('The quick brown fox', 'fox')); // "'fox' was found 1 times."
// console.log(searchWord('aa, bb, cc, dd, aa', 'aa')); // "'aa' was found 2 times."
const searchWord = (text, word) => {
let count = 0,
index;
text = text.toLowerCase();
word = word.toLowerCase();
while (index !== -1) {
index = text.indexOf(word, index);
if (index !== -1) {
count++;
index++;
}
}
return `'${word}' was found ${count} times.`;
};
console.log(searchWord('The quick brown fox', 'fox')); // "'fox' was found 1 times."
console.log(searchWord('aa, bb, cc, dd, aa', 'aa')); // "'aa' was found 2 times."

// Q14. Write a JavaScript function to test whether the character at the provided
(character) index is upper case?
// File name suggestion: isUpperCaseAt.js
// Test Data :
// console.log(isUpperCaseAt('Js STRING EXERCISES', 1)); // false
const isUpperCaseAt = (str, index) => str[index].toUpperCase() === str[index];
console.log(isUpperCaseAt('Js STRING EXERCISES', 1)); // false

// Q15. Write a JavaScript function to test whether the character at the provided
(character) index is upper case?
// File name suggestion: isLowerCaseAt.js
// Test Data :
// console.log(isLowerCaseAt('Js STRING EXERCISES', 1)); // true
const isLowerCaseAt = (str, index) => str[index].toLowerCase() === str[index];
console.log(isLowerCaseAt('Js STRING EXERCISES', 1)); // true

You might also like