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

180
Vistas
Could you please improve/shorten my JavaScript code for my son's 10th birthday?

My son is turning 10 and he's learning JavaScript. I am creating a birthday tarp for him and I want to use my below code. Do you think you can short it? Your input will be highly appreciated.

In my tarp, I will get rid of and

let age = 10, ten = 10;
while (true) {
  if (age == ten) {
    alert('Happy 10th Birthday, son!');
    break;
  } else {
    age++;
  }
}

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

0

let age = 10;
while (true) {
  if (age === 10) alert('Happy 10th Birthday, son!');
  else age++;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you're looking for something succinct but decorative, perhaps

let age = 0; // birthdate mm/dd/yyyy
while (age < 10) {
  age++
}
alert(`Happy ${age}th birthday, son!`);

The while condition keeps getting tested until it returns false; so it'll keep running as long as age is less than 10. Its last iteration happens at age == 9: 9 is less than 10, so it calls age++, now age is 10. It tries the why condition again, age is 10, so no longer less than 10, so it skips ahead to the alert.

Here's a runnable example with a lot more detail so you can see what it's doing:

let age = 0; 

// Pulling "age < 10" out into a function, so it can log what it's doing:
let checkAge = (age) => {
  console.log(`Checking if ${age} < 10: ${age < 10}`);
  return age < 10;
}


while (checkAge(age)) {
  console.log(`Will add 1 to ${age}`);
  age++;
  console.log(`Age is now ${age}`);
}
console.log(`Happy ${age}th birthday, son!`);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe this? Happy new year for your son :')

let age = 0;
while (age < 10) {
    age++;
}
alert('Happy 10th Birthday, son!');
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