This sprint challenge will test your understanding of the concepts covered in this sprint, including:
You will be required to:
Follow these steps to get started:
To submit your work:
Your submission will be evaluated based on:
To prepare for the sprint challenge, make sure you understand the following concepts and can solve similar problems:
Write a function that reverses the words in a string without using the built-in reverse()
method.
Example:
Input: "Hello World" Output: "World Hello"
Solution:
function reverseWords(str) {
// Split the string into an array of words
const words = str.split(' ');
// Create a new array to store words in reverse order
const reversedWords = [];
// Iterate through words array in reverse
for (let i = words.length - 1; i >= 0; i--) {
reversedWords.push(words[i]);
}
// Join the reversed words with spaces
return reversedWords.join(' ');
// One-liner alternative:
// return str.split(' ').reverse().join(' ');
}
console.log(reverseWords("Hello World")); // "World Hello"
console.log(reverseWords("JavaScript is fun")); // "fun is JavaScript"
Given an array containing n distinct numbers taken from 0 to n, find the missing number.
Example:
Input: [3,0,1] Output: 2 Input: [9,6,4,2,3,5,7,0,1] Output: 8
Solution:
function findMissingNumber(nums) {
// Calculate expected sum of numbers from 0 to n
const n = nums.length;
const expectedSum = (n * (n + 1)) / 2;
// Calculate actual sum of numbers in the array
const actualSum = nums.reduce((sum, num) => sum + num, 0);
// The difference is the missing number
return expectedSum - actualSum;
}
console.log(findMissingNumber([3,0,1])); // 2
console.log(findMissingNumber([9,6,4,2,3,5,7,0,1])); // 8
Write a function that counts the occurrences of each character in a string and returns an object with the results.
Example:
Input: "hello world" Output: { h: 1, e: 1, l: 3, o: 2, ' ': 1, w: 1, r: 1, d: 1 }
Solution:
function countCharacters(str) {
// Create an empty object to store character counts
const charCount = {};
// Iterate through each character in the string
for (let char of str) {
// If character is already in the object, increment count
if (charCount[char]) {
charCount[char]++;
}
// Otherwise, add character to object with count of 1
else {
charCount[char] = 1;
}
// Alternative using the || operator:
// charCount[char] = (charCount[char] || 0) + 1;
}
return charCount;
}
console.log(countCharacters("hello world"));
// { h: 1, e: 1, l: 3, o: 2, ' ': 1, w: 1, r: 1, d: 1 }
Complete the career readiness quiz to demonstrate your understanding of professional development concepts.