Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
Swap Cases- switch from lowercase to uppercase and viceversa

Given a string I am supposed to make all the lowercase letters to uppercase and vice versa. I tried this and but can not figure out why it is wrong. The output gives me: Can not read property .toLowerCase() of undefined.

    function SwapCase(str) { 
let cap=str.toUpperCase
let low=str.toLowerCase
let arr=str.split("")  
let result=[]

for(var i=0; i<=arr.length;i++){  
  for(var j=0; j<=cap.length;j++){ 
for( var k=0; k<=low.length;k++){ 
  if(arr[i]===cap[j]){ 
     result.push(arr[i].toLowerCase())
  } 
  if(arr[i]===low[k]){ 
     result.push(arr[i].toUpperCase())
  }
}
  

} } 
  return result.join(" ")
  } 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are multiple issues with this code,

  1. The array should not run to length, so it should be < instead of <=

  2. There is no need for 3 loops

  3. result.join should join with an empty string instead of blank space

  4. Intendations are all over the place.

see corrected code below:

function SwapCase(str) { 
  let cap=str.toUpperCase()
  let low=str.toLowerCase()
  let arr=str.split("")  
  let result=[]

  for(var i=0; i<arr.length;i++){  
    if(arr[i]===cap[i]){ 
      result.push(arr[i].toLowerCase())
    } 
    if(arr[i]===low[i]){ 
      result.push(arr[i].toUpperCase())
    }
  } 
  return result.join("")
}

console.log(SwapCase("heLLo"))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda