How do it maintain the object data type (TODA app program)

This is an understanding question not an coding mistake. In linked program there is something I don’t understand. My understanding is in javaScript it is very easy to change a data type, if can depending on the circumstance, can be as easy as changing a ‘let’ variable’s value.

However in this program in edit task function the currentTask which was defined as a empty object on line 14 is changed to what appears to have a numerical value rather than one or more key value pairs, which I would have assumed changes its data type.

However I assume this is wrong both due the succeeding line of code implying it is still an object and the the fact the code would assumably have done some else, presumably let currentTask; to initially declare the variable instead. Where I am I misunderstanding.

const taskForm = document.getElementById("task-form");
const confirmCloseDialog = document.getElementById("confirm-close-dialog");
const openTaskFormBtn = document.getElementById("open-task-form-btn");
const closeTaskFormBtn = document.getElementById("close-task-form-btn");
const addOrUpdateTaskBtn = document.getElementById("add-or-update-task-btn");
const cancelBtn = document.getElementById("cancel-btn");
const discardBtn = document.getElementById("discard-btn");
const tasksContainer = document.getElementById("tasks-container");
const titleInput = document.getElementById("title-input");
const dateInput = document.getElementById("date-input");
const descriptionInput = document.getElementById("description-input");

const taskData = [];
let currentTask = {};

const addOrUpdateTask = () => {
  const dataArrIndex = taskData.findIndex((item) => item.id === currentTask.id);
  const taskObj = {
    id: `${titleInput.value.toLowerCase().split(" ").join("-")}-${Date.now()}`,
    title: titleInput.value,
    date: dateInput.value,
    description: descriptionInput.value,
  };

  if (dataArrIndex === -1) {
    taskData.unshift(taskObj);
  }

  updateTaskContainer()
  reset()
};

const updateTaskContainer = () => {
  tasksContainer.innerHTML = "";

  taskData.forEach(
    ({ id, title, date, description }) => {
        tasksContainer.innerHTML += `
        <div class="task" id="${id}">
          <p><strong>Title:</strong> ${title}</p>
          <p><strong>Date:</strong> ${date}</p>
          <p><strong>Description:</strong> ${description}</p>
          <button onclick="editTask(this)" type="button" class="btn">Edit</button>
          <button onclick="deleteTask(this)" type="button" class="btn">Delete</button> 
        </div>
      `
    }
  );
};


const deleteTask = (buttonEl) => {
  const dataArrIndex = taskData.findIndex(
    (item) => item.id === buttonEl.parentElement.id
  );

  buttonEl.parentElement.remove();
  taskData.splice(dataArrIndex, 1);
}

const editTask = (buttonEl) => {
    const dataArrIndex = taskData.findIndex(
    (item) => item.id === buttonEl.parentElement.id
  );

  currentTask = taskData[dataArrIndex];

  titleInput.value = currentTask.title;
  dateInput.value = currentTask.date;
  descriptionInput.value = currentTask.description;

  addOrUpdateTaskBtn.innerText = "Update Task";
  
  
}

const reset = () => {
  titleInput.value = "";
  dateInput.value = "";
  descriptionInput.value = "";
  taskForm.classList.toggle("hidden");
  currentTask = {};
}

openTaskFormBtn.addEventListener("click", () =>
  taskForm.classList.toggle("hidden")
);

closeTaskFormBtn.addEventListener("click", () => {
  const formInputsContainValues = titleInput.value || dateInput.value || descriptionInput.value;
  if (formInputsContainValues) {
    confirmCloseDialog.showModal();
  } else {
    reset();
  }
});

cancelBtn.addEventListener("click", () => confirmCloseDialog.close());

discardBtn.addEventListener("click", () => {
  confirmCloseDialog.close();
  reset()
});

taskForm.addEventListener("submit", (e) => {
  e.preventDefault();

  addOrUpdateTask();
});

Small side question do I count as a view on a post I create.

Where do you think the currentTask is being changed to a numerical value, specifically?

1 Like
currentTask = taskData[dataArrIndex];

Why do you think that the taskData array contains numbers?

Think I may have worked it out it, is it a key value pair? I was getting confused by index and thinking it was A number(not an array). I also could not work how to use console.log statements to help in this case, otherwise I would have worked it out before now.

1 Like

I think I know the answer to this question by the fact a post of mine was edited by Jeremy and replying by him on one view. I must just be case that someone is always or nearly always, quick to see my posts.

by it I mean taskData[dataArrIndex]

Judging by your like it appears, the answer to my question is yes, however it is difficult to judge which comment is the solution so I won’t assign one but feel, Jeremy to.

This has sort of been covered elsewhere in the thread, but not clearly, so here are the solutions. On the point of if, I count as view, on a thread I create, no, posts generally get seen by someone quickly. On the main point I mistakenly thought the line, currentTask = taskData[dataArrIndex]; gave a numerical value (the index number) not a key value pair. Therefore there was no change in data type.

Ignore this comment for reasons that should be clear.