Reputation: 69
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. I'm trying to control it with the "toLowerCase" command inside. 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.
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();
}
Upvotes: 0
Views: 60
Reputation: 1
The todo you converted to lower case is not the one you added to your todo list
Note the double "e" at the end of each variable
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();
}
Upvotes: -1
Reputation: 1991
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
Upvotes: 1
Reputation: 138
In this case, todos.includes(newTodoValuee)
requires to be extended since your case is different.
Instead use this: if(todos.some(a=>a.trim().toLowerCase() == newTodoValuee))
Upvotes: 1