CC-08: String Mutation and Array Iteration

with Carmen Salas • 2024/10/16

Code Challenge

  1. Declare a function called stripUpperCase that takes in a string argument consistenting of only alphabet characters and returns a new string with all uppercase letters removed. Note: You should not alter the input str, this function should return a new string.

// this function is pure!
const stripUpperCase = (str) => {
  let res = str.split('');
  return res.filter((char) => {
    if (char == char.toLowerCase()) { return char; }
  }).join('');
}

// console.log('Hello');
// console.log(stripUpperCase('Hello')); // 'ello'
// console.log('SevenEleven');
// console.log(stripUpperCase('SevenEleven')); // 'evenleven'
// console.log('code challenge is fun');
// console.log(stripUpperCase("code challenge is fun")); // 'code challenge is fun'
  1. Declare a function named findLongestWord, that takes an array of strings as an argument, and returns the longest string in the array. If there is more than one longest string, the function should return the first longest string that appears in the array. You must solve this without using any array methods.

const findLongestWord = (arr) => {
  // without using array methods:
  // let res = '';
  // for (let word of arr) {
  //   if (word.length > res.length) { res = word; };
  // }
  // return res;
   
  // using reduce!
  return arr.reduce((res, curr) =>
   (curr.length > res.length) ? curr : res);
}

// console.log(findLongestWord(["The","quick","brown", "fox", "jumped", "over", "the", "lazy", "dog"]));
// console.log(findLongestWord(["jazzy", "jumpy", "quaky"])) //returns "jazzy"

Last updated