0

Want a code that will ask user to insert a number find inserted number in array and print out array without that number.

What is wrong with this one ?

var arr=[1,2,3,4];

var dWord=prompt("What word do you want to delete ?");

arr.forEach(function(find){
    if(dWord==arr[find]){
        var second=arr.slice(arr[find],arr.lenght-1);
        var first=arr.slice(0,arr[find]);
        second.shift();
        console.log(first.concat(second));  }
else{
    console.log("There is no that numberi array!") ;
}
})

2 Answers 2

2

This is what filter is for. You can use it to create a new array based on a boolean condition. In this case, that condition is whether or not any given word in the array matches the given deletion-word.

Based on your usage of the slice function, I'm not sure if you're asking to user for an index to be removed or a word to be removed.

If you're asking for them to input a word they don't want, then you can do that quite easily:

const arr = ['lorem', 'ipsum', 'dolor', 'si', 'amet'];
const dWord = 'ipsum' // prompt("What word do you want to delete?");

const result = arr.filter(word => word != dWord);
console.log(result);

If you're asking them for an index that they want removed, then you can use the optional second argument of the function that filter takes, as follows:

const arr = ['lorem', 'ipsum', 'dolor', 'si', 'amet'];
const dIndex = 2 // prompt("What word do you want to delete?");

const result = arr.filter((word, index) => index != dIndex);
console.log(result);


The problem with your original code is how you are using the forEach function. When you run

['a', 'b', 'c'].forEach(function(find) {
  // ...
});

You run a loop in which each iteration has 'a', 'b', or 'c' as find. In your case, your loop would run four times, with 1, 2, 3, and finally 4 as find. This isn't a problem until you then go back and index the original array with those numbers. The only reason it slightly worked is that your array was full of numbers. If it had been letters, you would have gotten an error. Then, tacking on the problems, the slice function expects to receive two indecies at which to make its cuts. You actually passed in the numbers from the array. So, on the first loop, your find is 1, which means that arr[find] is 2, so when you make the cut you're actually cutting at a completely unrelated index... it's a bit of a snowball of issues.

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

6 Comments

Thank you for help!
matthew this filter is assuming the index i and not the actual number.
@Bibberty based on the use of slice in the OP I assumed that's what Josip was after. To search for the actual number, the function just needs to be changed to e => e != dWord.
Thats fair, his ask contradicts his code. But it is the code he asking for help on. Maybe just provide both versions.
Good point. I originally had the element version. I will also add some explanation as to why his did not work...
|
1

You can use the filter function to do this:

var arr = [1, 2, 3, 4];

var dWord = prompt("What word do you want to delete ?");

var newArr = arr.filter(function(el) {
  if (el != dWord)
    return el;
});

console.log(JSON.stringify(newArr));

1 Comment

This usage of filter is a bit flimsy. Filter expects a truthy/falsey result, not the array element itself (although it evaluates to truthy, it's still a bit wonky to me). You seem to have implemented this for map but used filter instead.

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.