JavaScript Code Exercises 3

JavaScript Code Exercises 3

Exercise 1: Title case a sentence
Exercise 2: Find the largest number in an array
Exercise 3: Confirm all elements in an array are the same
Exercise 4: Count the number of vowels in a string

Exercise 1: Title case a sentence

Write a function that takes a sentence as input and returns a new sentence where the first letter of each word is capitalized.

function titleCase(str) {

  const words = str.toLowerCase().split(” “);

  for (let i = 0; i < words.length; i++) {

    words[i] = words[i][0].toUpperCase() + words[i].slice(1);

  }

  return words.join(” “);

}

console.log(titleCase(“hello world”)); // Output: “Hello World”

In this example, we first convert the input string to lowercase and split it into an array of words. We then use a for loop to capitalize the first letter of each word by taking the first letter of the word, converting it to uppercase, and then appending the rest of the word. Finally, we join the array of words back into a string with spaces between them.

Exercise 2: Find the largest number in an array

Write a function that takes an array of numbers as input and returns the largest number in the array.

function largestNumber(arr) {

  let max = arr[0];

  for (let num of arr) {

    if (num > max) {

      max = num;

    }

  }

  return max;

}

console.log(largestNumber([1, 2, 3, 4, 5])); // Output: 5

In this example, we initialize a variable called max to the first element in the array. We then use a for loop to iterate through the rest of the array, updating the value of max if we find a larger number. Finally, we return the largest number we found.

Exercise 3: Confirm all elements in an array are the same

Write a function that takes an array as input and returns true if all the elements in the array are the same, and false otherwise.

function allEqual(arr) {

  for (let i = 1; i < arr.length; i++) {

    if (arr[i] !== arr[0]) {

      return false;

    }

  }

  return true;

}

console.log(allEqual([1, 1, 1, 1])); // Output: true

console.log(allEqual([1, 2, 3, 4])); // Output: false

In this example, we use a for loop to compare each element in the array to the first element. If we find any element that is not equal to the first element, we return false. If we make it through the entire loop without finding any unequal elements, we return true.

Exercise 4: Count the number of vowels in a string

Write a function that takes a string as input and returns the number of vowels (a, e, i, o, u) in the string.

function countVowels(str) {

  const vowels = “aeiou”;

  let count = 0;

  for (let letter of str.toLowerCase()) {

    if (vowels.includes(letter)) {

      count++;

    }

  }

  return count;

}

console.log(countVowels(“hello world”)); // Output: 3

In this example, we first initialize a variable called vowels to a string containing all the vowels. We also initialize a count variable to 0. We then use a for loop to iterate through each letter in the input string, converting it to lowercase. If the letter is a vowel,