skip to Main Content

i´am currently doing this kata on codewars: https://www.codewars.com/kata/5202ef17a402dd033c000009/train/javascript

I tested my code in runJS and every by the tests of this kata expected outputs are correct, but when I paste my code into the editor from codewars and run it I get thee following error:

TypeError: Cannot read properties of undefined (reading 'toUpperCase')
    at /workspace/node/test.js:8:28
    at Array.forEach (<anonymous>)
    at titleCase (test.js:7:14)
    at Context.<anonymous> (test.js:38:19)
    at process.processImmediate (node:internal/timers:471:21)

Can someone explain me why I am getting this error in codewars but in runJS everything works properly?

function titleCase(title, minorWords) {
  if (title === "" && minorWords === "") return ""
  if (!minorWords) {
    let result1 = []
    let wordArr2 = title.split(" ")
    wordArr2.forEach((word) => {
      result1.push(word[0].toUpperCase() + word.substring(1).toLowerCase())
    })
    return result1.join(" ")
  }  
  
     let newTitle = title.toLowerCase()
     let newMinorWords = minorWords.toLowerCase()
     let wordArr = newTitle.split(" ")
     let minorWordArr = newMinorWords.split(" ")
     let result2 = []

     
     
   wordArr.forEach((word) => {
     if (!minorWordArr.includes(word) || minorWordArr.includes(word[0])) {
       result2.push(word[0].toUpperCase() + word.substring(1).toLowerCase())
     } else {
       result2.push(word.toLowerCase())
     }   
   })
  
return result2.join(" ") 

}

This is my code.

const Test = require('@codewars/test-compat');

describe("Tests", () => {
  it("test", () => {
Test.assertEquals(titleCase(''), '')
Test.assertEquals(titleCase('a clash of KINGS', 'a an the of'), 'A Clash of Kings')
Test.assertEquals(titleCase('THE WIND IN THE WILLOWS', 'The In'), 'The Wind in the Willows')
Test.assertEquals(titleCase('the quick brown fox'), 'The Quick Brown Fox')

});
});

And this are the tests on codewars.

Thank you in advance!

2

Answers


  1. There may be no second argument passed, in which case minorWords is undefined, not an empty string. You can set a default value for the parameter to ensure it is an empty string when not set.

    function titleCase(title, minorWords = "")
    

    Also, your solution does not consider that the first word should always be capitalized.

    wordArr.forEach((word, i) => {
        if (!minorWordArr.includes(word) || i === 0) {
    
    Login or Signup to reply.
  2. Two inputs are required for the titleCase function. try using TypeScript. very easy to detect such errors

    function titleCase(title, minorWords) {
      if (title === "" && minorWords === "") return ""
      if (!minorWords) {
        let result1 = []
        let wordArr2 = title.split(" ")
        wordArr2.forEach((word) => {
          result1.push(word[0].toUpperCase() + word.substring(1).toLowerCase())
        })
        return result1.join(" ")
      }  
      
         let newTitle = title.toLowerCase()
         let newMinorWords = minorWords.toLowerCase()
         let wordArr = newTitle.split(" ")
         let minorWordArr = newMinorWords.split(" ")
         let result2 = []
    
         
         
       wordArr.forEach((word) => {
         if (!minorWordArr.includes(word) || minorWordArr.includes(word[0])) {
           result2.push(word[0].toUpperCase() + word.substring(1).toLowerCase())
         } else {
           result2.push(word.toLowerCase())
         }   
       })
      
    return result2.join(" ") 
    
    }
    
    try{
    // error
    titleCase('') // Two inputs are required for this function
    }catch(e){
      console.log(e.message)
    }
    let res = titleCase('a clash of KINGS', 'a an the of')
    console.log(res)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search