1

I'm a beginner to vanilla JS and was building this program to remove a specific item from an array. However it's bit tricky.

let cart = ['bread', 'milk', 'rice'];


while (6 > 0){
    let remove = prompt("Enter The Item You Want to Remove");
    if (remove.toLowerCase() === "done"){
        break
    }
    else{
        for (let j = 0; j < cart.length; j++){
            if (remove.toLowerCase() === cart[j].toLowerCase()){
                cart.splice(cart[j],1);
            }
        }
    }
} 

Can you give me the solution to remove a specific item from an array. Thank You

4 Answers 4

2

let cart = ['milk', 'bread', 'rice'];

let removed = prompt("Type the item you want to remove");
let index = cart.indexOf(removed.toLowerCase())
if (index > -1) {
  cart.splice(index, 1);
}
console.log(cart);

Sign up to request clarification or add additional context in comments.

Comments

2

let cart = ['bread', 'milk', 'rice'];
    
    const index = cart.indexOf('bread');
    if (index > -1) {
      cart.splice(index, 1);
    }
    console.log(cart);

5 Comments

1- Ignoring the case is required. for example, MILK doesn't work. 2- We need to get the value from the user, not bypassing it. check my answer
@HakunaMatata I didn't write complete code where a user can copy and paste, as per question I tried to give the logic how we can implement it now validation and on demand value we can get as per our need.
Chill dude! no offense :) first, I didn't copy-paste any code, and I'm just explaining the request in this question.
@HakunaMatata sorry friend, you took my word other way :) My intention was, everyone should put some effort so that they can understand how the things are working not just copy and paste to make it work.
I'm sorry, English is my 4th language so I struggle sometimes. I didn't understand well. Sorry again.
2

Just refactored the "for loop" block code and added the validation for the input value.

let cart = ['bread', 'milk', 'rice'];

while (6 > 0) {
    let remove = prompt("Enter The Item You Want to Remove");
    if (remove.toLowerCase() === "done" && remove.toLowerCase() !== "") {
        console.log(cart)
        break
    }
    else {
        let index = cart.indexOf(remove);
        if (index > -1) {
            cart.splice(index, 1);
            console.log(cart);
        }
    }
    
}

1 Comment

Just refactored the "for loop" block code and added the validation for the input value.
0

Just pass index for the splice method.

let cart = ['bread', 'milk', 'rice'];


while (6 > 0){
    let remove = prompt("Enter The Item You Want to Remove");
    if (remove.toLowerCase() === "done"){
        console.log(cart)
        break
    }
    else{
        for (let j = 0; j < cart.length; j++){
            if (remove.toLowerCase() === cart[j].toLowerCase()){
                cart.splice(j,1);
            }
        }
    }
    
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.