I have two inputs, one where I choose a letter and one where I say how many of those letter occurrences to splice. Right now I am only able to delete all the occurrences of the letter I select from the input box, but I would like to be able to set how many items of the same letter I can delete.
For example:
I have an array like the one below and I would like to delete only the first "a". If I put 1 in my input, it should do that. If I put 2, then it should delete the first and the second "a" from the array.
It looks easy but I can't come up with a solution to set how many I want them deleted.
var letters = ["a","b","c","a"];
function deleteLetter() {
var valChosenLetter = document.getElementById("chosen-letter").value;
var valHowManyToDelete = document.getElementById("how-many-to-delete").value;
for (var i = letters.length -1; i >= 0; i--) {
if(letters[i] == valChosenLetter)
{
letters.splice(i, 1);
}
};
console.log(letters);
}
I did it manually but that's not the solution:
var letters = ["a","b","c","a"];
function deleteLetter() {
var valChosenLetter = document.getElementById("chosen-letter").value;
var valHowManyToDelete = document.getElementById("how-many-to-delete").value;
for (var i = letters.length -1; i >= 0; i--) {
if(letters[i] == valChosenLetter)
{
if(valHowManyToDelete < 2)
{
letters.splice(i, 1);
break;
}
else
{
letters.splice(i, 1);
}
}
};
console.log(letters);
}