Loops - Exercise 3
Question 1: Reverse Characters with a Loop Completed
Return the supplied ASCII string with its characters reversed using a loop.
Expected output: "edoc" (print this with console.log)
function reverseAsciiWithLoop(text) {
// Write your solution here
}
console.log(reverseAsciiWithLoop("code"));function reverseAsciiWithLoop(text) {
let result = "";
for (const char of text) result = char + result;
return result;
}
console.log(reverseAsciiWithLoop("code"));Question 2: Count Uppercase ASCII Letters Completed
Return the count of uppercase English letters A through Z in the supplied string.
Expected output: 3 (print this with console.log)
function countUppercaseAsciiLetters(text) {
// Write your solution here
}
console.log(countUppercaseAsciiLetters("Hi JS!"));function countUppercaseAsciiLetters(text) {
let count = 0;
for (const char of text) if (char >= "A" && char <= "Z") count++;
return count;
}
console.log(countUppercaseAsciiLetters("Hi JS!"));Question 3: Extract Digits from Text Completed
Return the ASCII digits in the input string in their original order.
Expected output: "123" (print this with console.log)
function extractAsciiDigits(text) {
// Write your solution here
}
console.log(extractAsciiDigits("a1b23"));function extractAsciiDigits(text) {
let result = "";
for (const char of text) if (char >= "0" && char <= "9") result += char;
return result;
}
console.log(extractAsciiDigits("a1b23"));Question 4: Count ASCII Vowels Completed
Count English vowels a, e, i, o, and u in the supplied text, ignoring case.
Expected output: 2 (print this with console.log)
function countAsciiVowels(text) {
// Write your solution here
}
console.log(countAsciiVowels("Hello"));function countAsciiVowels(text) {
let count = 0;
for (const char of text.toLowerCase()) if ("aeiou".includes(char)) count++;
return count;
}
console.log(countAsciiVowels("Hello"));Question 5: Find the First Space Completed
Return the index of the first space character, or -1 if the string has no spaces.
Expected output: 5 (print this with console.log)
function firstSpaceLoopIndex(text) {
// Write your solution here
}
console.log(firstSpaceLoopIndex("hello world"));function firstSpaceLoopIndex(text) {
for (let i = 0; i < text.length; i++) if (text[i] === " ") return i;
return -1;
}
console.log(firstSpaceLoopIndex("hello world"));Question 6: Alternate ASCII Letter Case Completed
Return an ASCII string where characters at even indices are uppercase and characters at odd indices are lowercase.
Expected output: "HeLlO" (print this with console.log)
function alternateAsciiCase(text) {
// Write your solution here
}
console.log(alternateAsciiCase("hello"));function alternateAsciiCase(text) {
let result = "";
for (let i = 0; i < text.length; i++) result += i % 2 === 0 ? text[i].toUpperCase() : text[i].toLowerCase();
return result;
}
console.log(alternateAsciiCase("hello"));Question 7: Join Words with a Loop Completed
Join an array of strings using a hyphen between entries and no trailing hyphen.
Expected output: "red-blue-green" (print this with console.log)
function joinWordsWithLoop(words) {
// Write your solution here
}
console.log(joinWordsWithLoop(["red","blue","green"]));function joinWordsWithLoop(words) {
let result = "";
for (let i = 0; i < words.length; i++) { if (i > 0) result += "-"; result += words[i]; }
return result;
}
console.log(joinWordsWithLoop(["red","blue","green"]));Question 8: Count Consecutive Spaces Completed
Return the length of the longest run of consecutive space characters in the input.
Expected output: 3 (print this with console.log)
function longestSpaceRun(text) {
// Write your solution here
}
console.log(longestSpaceRun("a b c"));function longestSpaceRun(text) {
let longest = 0;
let current = 0;
for (const char of text) { current = char === " " ? current + 1 : 0; if (current > longest) longest = current; }
return longest;
}
console.log(longestSpaceRun("a b c"));Question 9: Find the Last Digit Completed
Return the last ASCII digit in the string, or null if none exists.
Expected output: "7" (print this with console.log)
function lastAsciiDigit(text) {
// Write your solution here
}
console.log(lastAsciiDigit("x2y7z"));function lastAsciiDigit(text) {
for (let i = text.length - 1; i >= 0; i--) if (text[i] >= "0" && text[i] <= "9") return text[i];
return null;
}
console.log(lastAsciiDigit("x2y7z"));Question 10: Remove Spaces with a Loop Completed
Return the input string without any space characters. Other whitespace is unchanged.
Expected output: "abc" (print this with console.log)
function removePlainSpaces(text) {
// Write your solution here
}
console.log(removePlainSpaces("a b c"));function removePlainSpaces(text) {
let result = "";
for (const char of text) if (char !== " ") result += char;
return result;
}
console.log(removePlainSpaces("a b c"));