String Flashcards
const sentence = ‘The quick brown fox jumps over the lazy dog.’;
const index = 4;
console.log(The character at index ${index} is ${sentence.charAt(index)}
);
// expected output: “The character at index 4 is q”
const str1 = 'Hello'; const str2 = 'World';
console. log(str1.concat(‘ ‘, str2));
console. log(str2.concat(‘, ‘, str1));
// expected output: “Hello World”
// expected output: “World, Hello”
const str1 = ‘Cats are the best!’;
console.log(str1.endsWith(‘best’, 17));
const str2 = ‘Is this a question’;
console.log(str2.endsWith(‘?’));
// expected output: true
// expected output: false
const sentence = ‘The quick brown fox jumps over the lazy dog.’;
const word = ‘fox’;
console.log(The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence
);
// expected output: “The word “fox” is in the sentence”
const paragraph = ‘The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?’;
const searchTerm = 'dog'; const indexOfFirst = paragraph.indexOf(searchTerm);
console. log(The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}
);
console. log(The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}
);
// expected output: “The index of the first “dog” from the beginning is 40”
// expected output: “The index of the 2nd “dog” is 52”
const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; const regex = /[A-Z]/g; const found = paragraph.match(regex);
console.log(found);
// expected output: Array [“T”, “I”]
const str = ‘The quick brown fox jumps over the lazy dog.’;
const words = str.split(' '); console.log(words[3]);
const chars = str.split(''); console.log(chars[8]);
const strCopy = str.split(); console.log(strCopy);
// expected output: “fox”
// expected output: “k”
// expected output: Array [“The quick brown fox jumps over the lazy dog.”]
const str = ‘The quick brown fox jumps over the lazy dog.’;
console. log(str.slice(31));
console. log(str.slice(4, 19));
console. log(str.slice(-4));
console. log(str.slice(-9, -5));
// expected output: “the lazy dog.”
// expected output: “quick brown fox”
// expected output: “dog.”
// expected output: “lazy”
const str = ‘Mozilla’;
console. log(str.substring(1, 3));
console. log(str.substring(2));
// expected output: “oz”
// expected output: “zilla”
const str = ‘Mozilla’;
console. log(str.substring(1, 3));
console. log(str.substring(2));
// expected output: “oz”
// expected output: “zilla”