为什么如果我决定再次运行addStar()函数,它不起作用?

huangapple go评论84阅读模式
英文:

Why is my addStar() function not working if i decide to run it again for the second time?

问题

这个项目是一个类似联系人应用的项目,使用JavaScript编写,具备添加联系人、将联系人添加到收藏夹、编辑联系人、搜索、更改应用程序主题和删除联系人等功能。

问题似乎出现在addStar()函数中。您提到,无法再次将保存的联系人添加到favArray,即使在之前将其从中移除。在单击星形按钮时,addStar()函数不会运行,直到关闭描述页面。

您可以尝试以下方法来解决这个问题:

  1. 确保starClicked的初始值为false,以确保addStar()函数正确地运行。

  2. 检查在单击星形按钮后,currentContact的值是否仍然是正确的联系人对象。

  3. 考虑在添加和删除联系人时,使用console.log()来调试,以查看每个步骤的执行情况,从而找出问题所在。

  4. 检查是否有其他与点击星形按钮相关的事件监听器或代码可能干扰了addStar()函数的正常工作。

这些方法可能有助于您找到并解决问题。如果问题仍然存在,您可能需要提供更多关于问题的详细信息,以便更好地帮助您解决它。

英文:

I am into JavaScript and while attempting a project that will be powered by JavaScript I got stuck.

This project is supposed to work like a contact Application that works on a mobile phone with capabilities to add a contact, add the contact to favourite, edit the contact, search, change the theme of the application and delete the contact.

I have reached the point where you can add a contact but when i created the functionality to add the contact to favourite- If i add a contact to the favArray and remove it from the favArray and I change my mind and I try to add it back to the favArray it will not work(this i do by clicking the star button on the page that appears when you click the created contact) . I have tried debugging this but it just seems to me that i have been stuck.

I am unable to add a saved contact to the favArrayagain after i have added it to the favArray and removed it from the favArray- Humbly, from my point of view, I think the problem is coming from the addStar() function.

// META CAPABILITIES TO ADD TO FAVOURITES; EDIT THE CONTACT; AND DELETE THE CONTACT

const star = document.querySelector('.star');
star.innerHTML = '<i class="fa-sharp fa-regular fa-star"></i>';
// const edit = document.querySelector('.edit');
// const removeCon = document.querySelector('.delete');
    
function addStar(){
    starClicked = true;
    if (star.innerHTML === '<i class="fa-sharp fa-regular fa-star"></i>') {
        favArray.push(currentContact);
        starClicked = false;
        star.innerHTML = '<i class="fa-solid fa-star"></i>';
    } else  if (favArray.includes(currentContact) && star.innerHTML === '<i class="fa-solid fa-star"></i>') {
        const index = favArray.indexOf(currentContact);
        favArray.splice(index, 1);
        star.innerHTML = '<i class="fa-regular fa-star"></i>';
    }
}      
star.addEventListener('click', addStar);

I expected my console to give me a particular error so I checked the console but to my dismay, there was no error.

I tried building the addStar() function from scratch but it did not work. After doing this i expected that whenever i click the star on the description page it should work but it was adding the contact to the favArray and if i click the star it will remove it from favArray and after i have removed it, if i decide to add it back to the favArray by clicking the star it will not work.

And as suggested by Members of this great community I also tried putting the addStar() function and the doc() function outside the loop that loops through the entire contactArray.
Also I tried adding breakpoints and checking if it enters the correct branch then I realised that after adding it to the favArray for the first time and I remove it from the favArray and i decide not to close the description page and still change my mind and try to add it back to the favArray-** the addStar() function will not run until i close the description page.**
I have also tried asking ChatGpt about it but it gave me a code that introduced more bugs and I am a person that likes implementing what i understand.

答案1

得分: 0

在函数updateContacts中,你有一个循环遍历所有联系人的部分:

function updateContactList() {
  ...
  for (let i = 0; i < contactArray.length; i++) {

在代码的后面,在同一个循环中,你在addStar函数中使用了i来获取特定索引的联系人:

function addStar() {
  ...
  const index = favArray.indexOf(contactArray[i]);
  ...
}

之后,你为star图标添加了一个click事件处理程序:

star.addEventListener('click', addStar);

现在,让我们想象一下这是如何工作的。
在循环的每次迭代中,你都会创建一个具有特定i的新实例的addStar,并将其附加到相同的单个星标图标上。

结果是,当你点击星标图标时,所有联系人的所有addStar函数都将被执行。

你可以通过在addStar()函数的第一行添加console.log('addStar', i);来轻松测试这一点,在其中创建3个联系人,打开其中任何一个,并将其标记为星标。打开控制台并检查你看到了什么。

现在让我们考虑如何解决这个问题。

我建议你稍微了解一下模型-视图-控制器(MVC)概念,以了解model(即状态)和view(其可视化表示)的概念。在你的代码中,所有东西都混在一起,这使得以结构化的方式管理modelview变得困难。理想情况下,你应该能够完全分离修改HTML的代码和管理联系人列表的代码(作为对象数组,即你的模型)。

根据当前的方法,你现在需要做什么。

你需要将addStarviewContact(称为doc)函数移出循环。你不需要为每个联系人制作这些函数的副本。对于这些函数要正常工作,你需要知道用户点击的currentContact。在循环本身中,你只创建联系人并附加一个点击处理程序。该点击处理程序应该将currentContact设置为用户点击的联系人,并调用viewContact。在viewContact内部,用户将能够点击star图标,而addStar将使用全局变量currentContact来将其值添加或从收藏夹中删除。

请让我知道这是否有帮助。

英文:

Within the function updateContacts you have a loop that goes through all the contacts:

function updateContactList() {
...
for (let i = 0; i < contactArray.length; i++) {

Later in the code, within the same loop, you use this i within the addStar function to get the contact with a specific index:

function addStar() {
...
const index = favArray.indexOf(contactArray[i]);
...
}

After that, you add a click handler for the star icon:

star.addEventListener('click', addStar);

Now let's imagine how this work.
In every iteration of the loop, you kind of create a new instance of addStar with a specific i and attach it to the same single star icon.

The result is that when you click the star icon, all addStar functions for all contacts will be executed.

You can easily test this by adding console.log('addStar', i); as a very first line within the addStar() function, create 3 contacts, open any of them, and star it. Open the console and check what you see.

Now let's think about how to fix this problem.

I would recommend you to read a bit about Model-View-Controller concept to get an idea of model (aka state) and view (its visual re-presentation). In your code, everything is mixed all together, and it makes it hard to manage a model and a view in a structured way. Ideally, you should be able to completely separate the code that modifies HTML from the code that manages the list of contacts as an array of objects (your model).

What you need to do now with the current approach.

You need to move the addStar and viewContact (called doc) function out of the loop. You don't need to make a copy of these functions for each contact. For these functions to work, you need to know the currentContact the user clicked. In the loop itself, you only create contacts and attach a click handler. That click handler should set currentContact to the contact clicked and call viewContact. Within viewContact user will be able to click the star icon, and addStar will use the currentContact global variable to add or remove its value from favorites.

Please let me know if this helps.

huangapple
  • 本文由 发表于 2023年5月28日 22:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定