Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Cómo recorrer la matriz de días a partir de la fecha actual

Creé una aplicación simple que imprime días desde una matriz. Cuando hace clic en el botón, imprime otro día y otro, etc. Sin embargo, cuando agrego un punto de partida, el día actual, no puedo recorrer los días siguientes.

 const days = [ "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday ", "Sunday ", ]; const btn = document.querySelector("#btn"); let d = new Date(); let currentDay = d.getDay(); const list = document.querySelector("#list"); let i = 0; btn.addEventListener("click", function () { let newEl = document.createElement("li"); newEl.textContent = days[currentDay]; // this is the place where I'm stuck // I cannot write: days[currentDay]++ i++; if (i >= days.length) i = 0; list.append(newEl); });
 <h2>Days of the week:</h2> <ul id="list"></ul> <button id="btn">Add one more day</button>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

El método getDay() devuelve el día de la semana donde 0 representa el domingo

Quizás todo lo que tiene que hacer es hacer que el domingo sea el primer elemento de la matriz.

Por favor, intente el siguiente código

 const days = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", ]; var currentDay = new Date().getDay(); // Sun = 0, Mon = 1, ..., Sat = 6 console.log(days[currentDay]); console.log(days[currentDay + 1]);

Espero no haber entendido mal tu pregunta

fabio

about 4 years ago · Juan Pablo Isaza Report

0

Estás usando days[currentDay] . Pero como currentDay no cambia, no funcionará


Vamos de esta manera:

  1. Después de obtener currentDay , establezca i en currentDay (esto está bien ya que su matriz tiene el orden correcto, también podríamos usar indexOf .

  2. Use i como índice para mostrar la fecha:

 const days = [ "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday ", "Sunday ", ]; const btn = document.querySelector("#btn"); let d = new Date(); let currentDay = d.getDay(); const list = document.querySelector("#list"); let i = currentDay; btn.addEventListener("click", function () { let newEl = document.createElement("li"); newEl.textContent = days[i]; i++; if (i >= days.length) i = 0; list.append(newEl); });
 <h2>Days of the week:</h2> <ul id="list"></ul> <button id="btn">Add one more day</button>

about 4 years ago · Juan Pablo Isaza Report

0

El problema estaba en la ubicación de ++ que estabas intentando. Además, no necesita una variable adicional i

 const days = [ "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday ", "Sunday ", ]; const btn = document.querySelector("#btn"); let d = new Date(); let currentDay = d.getDay(); const list = document.querySelector("#list"); btn.addEventListener("click", function () { let newEl = document.createElement("li"); newEl.textContent = days[currentDay++]; if (currentDay >= days.length) currentDay = 0; list.append(newEl); });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!