// Q1. Write a JavaScript function to check whether a string is blank or not?
// File name suggestion: [Link]
// Test Data :
// [Link](isBlank('')); // true
// [Link](isBlank('abc')); // false
const isBlank = input => ![Link];
[Link](isBlank('')); // true
[Link](isBlank('abc')); // false
// Q2. Write a JavaScript function to split a string (sentence) and convert it into an
array of words?
// File name suggestion: [Link]
// Test Data :
// [Link](stringToArray("Robin Singh")); // ["Robin", "Singh"]
const stringToArray = str => [Link](' ');
[Link](stringToArray('Robin Singh')); // ["Robin", "Singh"]
// Q3. Write a JavaScript function to extract a specified number of characters from a
string?
// File name suggestion: [Link]
// Test Data :
// [Link](truncateString("Robin Singh", 4)); // "Robi"
const truncateString = (str, length) => [Link](0, length);
[Link](truncateString('Robin Singh', 4)); // "Robi"
// Q4. Write a JavaScript function to parameterize a string?
// File name suggestion: [Link]
// Test Data :
// [Link](stringParameterize("Robin Singh from USA.")); // "robin-singh-from-
usa"
const stringParameterize = str => [Link]().replace(/\s/g, '-');
[Link](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: [Link]
// Test Data :
// [Link](capitalize('js string exercises')); // "Js string exercises"
const capitalize = str => `${[Link](0).toUpperCase()}${[Link](1)}`;
[Link](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: [Link]
// Test Data :
// [Link](capitalizeWords('js string exercises')); // "Js String Exercises"
const capitalizeWords = str =>
str
.split(' ')
.map(word => `${[Link](0).toUpperCase()}${[Link](1)}`)
.join(' ');
[Link](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: [Link]
// Test Data :
// [Link](swapCase('AaBbc')); // "aAbBC"
const swapCase = str => {
let result = '';
for (let i = 0; i < [Link]; i++) {
if (str[i] === str[i].toUpperCase()) {
result += str[i].toLowerCase();
} else {
result += str[i].toUpperCase();
}
}
return result;
};
[Link](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: [Link]
// Test Data :
// [Link](alternateCase('samsung')); // "SaMsUnG"
const alternateCase = str => {
let result = '';
for (let i = 0; i < [Link]; i++) {
if (i % 2 === 0) {
result += str[i].toUpperCase();
} else {
result += str[i].toLowerCase();
}
}
return result;
};
[Link](alternateCase('samsung')); // "SaMsUnG"
// Q9. Write a JavaScript function to concatenates a given string n times (default is
1)?
// File name suggestion: [Link]
// Test Data :
// [Link](repeat('Ha!')); // "Ha!"
// [Link](repeat('Ha!',2)); // "Ha!Ha!"
// [Link](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;
};
[Link](repeat('Ha!')); // "Ha!"
[Link](repeat('Ha!', 2)); // "Ha!Ha!"
[Link](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: [Link]
// Test Data :
// [Link](insert('We are doing some exercises.')); // "We are doing some
exercises."
// [Link](insert('We are doing some exercises.','JavaScript ')); // "JavaScript We
are doing some exercises."
// [Link](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 `${[Link](0, n)}${ins_str}${[Link](n)}`;
};
[Link](insert('We are doing some exercises.')); // "We are doing some
exercises."
[Link](insert('We are doing some exercises.', 'JavaScript ')); // "JavaScript We
are doing some exercises."
[Link](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: [Link]
// Test Data :
// [Link](stringChop('w3resource')); // ["w3resource"]
// [Link](stringChop('w3resource',2)); // ["w3", "re", "so", "ur", "ce"]
// [Link](stringChop('w3resource',3)); // ["w3r", "eso", "urc", "e"]
const stringChop = (str, size = 1) => {
const result = [];
let smallStr = '';
for (let char of str) {
if ([Link] === size) {
[Link](smallStr);
smallStr = '';
}
smallStr += char;
}
if ([Link]) {
[Link](smallStr);
}
return result;
};
[Link](stringChop('w3resource')); // ["w3resource"]
[Link](stringChop('w3resource', 2)); // ["w3", "re", "so", "ur", "ce"]
[Link](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: [Link]
// Test Data :
// [Link](count("The quick brown fox jumps over the lazy dog", 'the')); // 2
const count = (text, word) => {
let count = 0,
index;
text = [Link]();
word = [Link]();
while (index !== -1) {
index = [Link](word, index);
if (index !== -1) {
count++;
index++;
}
}
return count;
};
[Link](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: [Link]
// Test Data :
// [Link](searchWord('The quick brown fox', 'fox')); // "'fox' was found 1 times."
// [Link](searchWord('aa, bb, cc, dd, aa', 'aa')); // "'aa' was found 2 times."
const searchWord = (text, word) => {
let count = 0,
index;
text = [Link]();
word = [Link]();
while (index !== -1) {
index = [Link](word, index);
if (index !== -1) {
count++;
index++;
}
}
return `'${word}' was found ${count} times.`;
};
[Link](searchWord('The quick brown fox', 'fox')); // "'fox' was found 1 times."
[Link](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: [Link]
// Test Data :
// [Link](isUpperCaseAt('Js STRING EXERCISES', 1)); // false
const isUpperCaseAt = (str, index) => str[index].toUpperCase() === str[index];
[Link](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: [Link]
// Test Data :
// [Link](isLowerCaseAt('Js STRING EXERCISES', 1)); // true
const isLowerCaseAt = (str, index) => str[index].toLowerCase() === str[index];
[Link](isLowerCaseAt('Js STRING EXERCISES', 1)); // true