简体   繁体   English

如何解决大小写检查器的问题?

[英]How do I fix the problem with the capitalization checker?

I have a one problem.我有一个问题。 This my TodoList Project: https://codepen.io/BerkayAkgurgen/pen/OJRqjPg (Please Check The Link İf you want to better understand the problem.) I'm trying to prevent the same words from being added to the list as items.这是我的 TodoList 项目: https://codepen.io/BerkayAkgurgen/pen/OJRqjPg (如果您想更好地理解问题,请查看链接。)我试图防止将相同的单词作为项目添加到列表中. I'm trying to control it with the "toLowerCase" command inside.我试图用里面的“toLowerCase”命令来控制它。 But it keeps adding the same words because their letter sizes are different, for example: "Berkay" "berkay" I want these two words not to be included even if their letter sizes are different.但它不断添加相同的单词,因为它们的字母大小不同,例如:“Berkay”“berkay”即使它们的字母大小不同,我也希望不要包含这两个单词。

 function addTodo(e) {
    const newTodoValue = todoInput.value.trim();
    const newTodoValuee = todoInput.value.trim().toLowerCase();
    let todos = getTodosFromStorage();
    if (newTodoValue === "") {
        console.log("merhaba");
    } else if (todos.includes(newTodoValuee)) {
        e.preventDefault();
        console.log("Aynı Todo");
        todoInput.value = "";
        return false;
    } else {
        addTodoToUI(newTodoValue);
        addTodoToStorage(newTodoValue);
    }
    e.preventDefault();
}

In this case, todos.includes(newTodoValuee) requires to be extended since your case is different.在这种情况下, todos.includes(newTodoValuee)需要扩展,因为您的情况不同。

Instead use this: if(todos.some(a=>a.trim().toLowerCase() == newTodoValuee))而是使用这个: if(todos.some(a=>a.trim().toLowerCase() == newTodoValuee))

In your code you are checking for lowercase but you are not converting the words in your array to lowercase before hand.在您的代码中,您正在检查小写字母,但您没有事先将数组中的单词转换为小写字母。

//Functions

function addTodo(e) {
    const newTodoValue = todoInput.value.trim();
    const lowerCaseTodoValue = todoInput.value.trim().toLowerCase();
    let todos = getTodosFromStorage();

  //should now convert to lowercase use this for comparisons
  var words = todos.map(w => w.toLowerCase());
  
  
    if (newTodoValue === "") {
        console.log("merhaba");
    } else if (words.includes(lowerCaseTodoValue)) {
        e.preventDefault();
        console.log("Aynı Todo");
        todoInput.value = "";
        return false;
    } else {
        addTodoToUI(newTodoValue);
        addTodoToStorage(newTodoValue);
    }
    e.preventDefault();
}

This should resolve your issue这应该可以解决您的问题

The todo you converted to lower case is not the one you added to your todo list您转换为小写的待办事项不是您添加到待办事项列表中的待办事项

  • You converted your todo to lowercase with variable newTodoValuee您使用变量newTodoValuee将待办事项转换为小写
  • You saved something different newTodoValue你保存了一些不同的东西newTodoValue

Note the double "e" at the end of each variable注意每个变量末尾的双“e”

This means that the lowercase todo you're checking for in your todo list is never there except it was saved as lowercase这意味着您在待办事项列表中检查的小写待办事项永远不会存在,除非它被保存为小写

Simply change the variable只需更改变量

function addTodo(e) {
    const newTodoValue = todoInput.value.trim();
    const newTodoValuee = todoInput.value.trim().toLowerCase();
    let todos = getTodosFromStorage();
    if (newTodoValue === "") {
        console.log("merhaba");
    } else if (todos.includes(newTodoValuee)) {
        e.preventDefault();
        console.log("Aynı Todo");
        todoInput.value = "";
        return false;
    } else {
        addTodoToUI(newTodoValue);
        // addTodoToStorage(newTodoValue);  //change the variable here to the one that end with "ee"
       addTodoToStorage(newTodoValuee);
    }
    e.preventDefault();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM