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

103
Vistas
Clever Programmer Javascript tutorial Age in Days Challenge

For the age in Days challenge in this video, I'm getting the following error:

Uncaught TypeError: Cannot read properties of null (reading 'appendChild') at yearBorn (script.js:10:47) at        HTMLButtonElement.onclick (challenge.html:19:64) yearBorn @ script.js:10 onclick @ challenge.html:19

      

JS:

function yearBorn() {

var birthYear = prompt('What year were you born?');
var ageInDays = (2022 - birthYear) \* 365;
var h1 = document.createElement('h1');    
var textAnswer = document.createTextNode('You are ' + ageInDays + ' days old');
h1.setAttribute('id', 'yearBorn');
h1.appendChild(textAnswer);
document.getElementById('flex-box-result').appendChild(h1);

html:

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />

    <title>Javascript Challenge</title>
    <link rel="stylesheet" href="static/css/index.css"
  </head>
  <body>
    <div class="container-1">
      <h2>Challenge 1: Your Age in Days</h2>
      <div class="flex-box-container-1">
        <div>
          <button class="btn btn-primary" onclick="yearBorn()">Click Me</button>
        </div>

        <div>
          <button class="btn btn-danger">Reset</button>
        </div>
      </div>
      <div class="flex-box-container-1">
        <div class="flex-box-result"></div>
      </div>
    </div>
    <script src="static/js/script.js"></script>
  </body>
</html>

Why am I getting the error?

I was expecting the age in days to print to the flex box container below the buttons in the document. Instead, getting the error

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

0

Solution 1 The error in this line

document.getElementById('flex-box-result').appendChild(h1)

Because document.getElementById() need an id which you provide when creating html element like this

<div id="id"></div>

Your flex-box-result is a class

<div class="flex-box-result"></div>

to

<div class="flex-box-result"></div>

Solution 2 In JavaScript code, you can also use a querySelector method with the selector of your element (like CSS selector)

document.querySelector('.flex-box-result').appendChild(h1)
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