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

266
Vistas
Array[position] returning NaN when it should be returning a string

The Problem: You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.

My Solution

function isOdd(num) {
  return num % 2;
}

function getMiddle(str) {
  const middleDigit = (str.length + 1) / 2;
  if (isOdd(middleDigit) === 1) {
    return str[middleDigit];
  } else {
    return str[middleDigit - 0.5] + str[middleDigit + 0.5];
  }
}
console.log(getMiddle(`the`));

But i'm receiving a NaN output, rather than h , where has str[input] deviated from the my intention?

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Some annotations:

  • Return a boolean for a function which starts with is....
  • Take the floored middle index without an offset of one.
  • Check the string length instead of the middle index without having an integer value.
  • Return either the single character or slice the string with adjustmens for start and end.

function isOdd(num) {
  return num % 2 === 1;
}

function getMiddle(str) {
    const middleDigit = Math.floor(str.length / 2);
    return isOdd(str.length)
        ? str[middleDigit]
        : str.slice(middleDigit - 1, middleDigit + 1);
}

console.log(getMiddle('the'));
console.log(getMiddle('boot'));

about 4 years ago · Juan Pablo Isaza Denunciar

0

you are passing the wrong value to is_odd

function isOdd(num) {
    return ((num % 2) === 1);
}

function getMiddle(str) {
    const middleDigit = Math.floor(str.length / 2);
    if (isOdd(str.length)) {
        return str[middleDigit];
    } else {
        return str[middleDigit - 1] + str[middleDigit];
    }
}
console.log(getMiddle(`they`));
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your execution was a little bit off!

  • I changed your isOdd function to return a boolean value, instead of a number.
  • After every calculation of the middle digit I subtracted 1 from the result, as we are working with indexes (and they start the count of the positions at 0, instead of 1).
  • For the second middle digit when the word length is even, you just have to do "str.length/2", no need for adding or subtracting 1.

    function isOdd(num) {
      return num % 2 === 1;
    }
    
    function getMiddle(str) {
      if (isOdd(str.length)) {
        return str[((str.length + 1) / 2) - 1];
      } else {
        return str[(str.length / 2) - 1] + str[str.length / 2];
      }
    }
    console.log(getMiddle(`the`));
    console.log(getMiddle(`root`));

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