Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

545
Visualizações
Javascript Serial Number CoderByte problem

I'm currently solving a problem at CoderByte. Here's the scenario.

Have the function SerialNumber(str) take the str parameter being passed and determine if it is a valid serial number with the following constraints:

  1. It needs to contain three sets each with three digits (1 through 9) separated by a period.
  2. The first set of digits must add up to an even number.
  3. The second set of digits must add up to an odd number.
  4. The last digit in each set must be larger than the two previous digits in the same set.

If all the above constraints are met within the string, the your program should return the string true, otherwise your program should return the string false. For example: if str is "224.315.218" then your program should return "true".

Examples
Input: "11.124.667"
Output: false

Input: "114.568.112"
Output: true

Here's my JS code for this problem.

function SerialNumber(str) { 

  // code goes here
  if(str.length < 11) {
    return "false";
  }

  for (let x = 0; x < str.length; x++) {
    if(str[x] === '0') {
      return "false";
    }
  }
  
  first = str[0] + str[1] + str[2];
  second = str[4] + str[5]+ str[6];

  if (first % 2 !== 0 || second % 2 === 0) {
    return "false";
  }

  if (str[2] < str[1] || str[2] < str[0] || str[6] < str[5] || str[6] < str[4] || str[10] < str[8] || str[10] < str[9]) {
    return "false";
  } else {
    return "true";
  }
}

When I input "11.124.667", it will return to false (which is correct). And when I input "114.568.112", it will also return to false (which is not correct). Any tips on how can I obtain this? Thanks for the feedback.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I tried your code and the problem comes from the third return false for the 2nd example, when you set second, you concatenate strings instead of adding numbers, thus %2 is always equal to 0 since
I added console.log before each return false to see which condition failed.
Here's the fixed code in which I added parseInt for first and second

function SerialNumber(str) {

    if (str.length < 11) {
        console.log("first")
        return "false";
    }

    for (let x = 0; x < str.length; x++) {
        if (str[x] === '0') {
            console.log("second")
            return "false";
        }
    }

    const first = parseInt(str[0]) + parseInt(str[1]) + parseInt(str[2]);
    const second = parseInt(str[4]) + parseInt(str[5]) + parseInt(str[6]);

    if (first % 2 !== 0 || second % 2 === 0) {
        console.log("third")
        return "false";
    }

    if (str[2] < str[1] || str[2] < str[0] || str[6] < str[5] || str[6] < str[4] || str[10] < str[8] || str[10] < str[9]) {
        console.log("fourth")
        return "false";
    } else {
        return "true";
    }
}

console.log("11.124.667 : " + SerialNumber("11.124.667"));
console.log("114.568.112 : " + SerialNumber("114.568.112"))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda