-1

I am trying to loop through an array, and if an element is equal to a set of values I want to update that element. So for example:

var arr = ["apple", "orange", "banana", "kiwi"];
for(var x= 1; x < arr.length; x++) {
  if(arr[x] === kiwi" || "banana") {
    arr[x] = arr[x] + "1";
  }
}

Expected output

arr = ["apple", "orange", "banana1", "kiwi1"];

I can't seem to get this right.

EDIT: Sorry about the typo's the code copied over wrong, I have fixed the typo's.

R_Lothbrok
  • 37
  • 1
  • 10

4 Answers4

3

This is your code:

var arr = ["apple", "orange", "banana", "kiwi"];
for(x = i; x < arr.length; x++) {
  if(arr[x] === kiwi" || "banana") {
    arr[x] = arr[x] + "1";
  }
}

Let's analize your approach:

You're using undeclared variable i.

for(x = i; x < arr.length; x++) {
        ^

You're not using in right way the logical operators

if(arr[x] === kiwi" || "banana")
                     ^

This code snippet has the fixes in your code:

var arr = ["apple", "orange", "banana", "kiwi"];
// most of the world use "i" as index in an array:
for(var i = 0/*Initialized with 0*/; i < arr.length; i++) {
    // Now the code is using two side comparison using OR (||) operator.
    if(arr[i] === "kiwi" || arr[i] === "banana") {
        arr[i] = arr[i] + "1";
    }
}

console.log(arr);

See? now it's working your logic!

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Ele
  • 33,468
  • 7
  • 37
  • 75
1

First of all, "bananna" will never be false, so arr[x] === kiwi" || "banana" will always be true. In English you might say "if item equals kiwi or banana" but in code you need to be more specific: "if item equals kiwi or item equals banana" (arr[x] === "kiwi" || arr[x] === "banana").

If you want to check against a group, it would be easier to make an array and check to see if the item is in the array instead. if(~["kiwi","banana"].indexOf(arr[x])) In English this says: "If item exists in this group." indexOf() returns -1 if the item does not exist, but by using the bitwise NOT (~) the -1 becomes a zero, which is false-y. If this is too much to understand you could just do if(["kiwi","banana"].indexOf(arr[x]) > -1) instead.

One more thing... make sure you're checking the console for errors. You won't get very far unless you become really familiar with the console. It will tell you about things like missing quotes and other issues you have going on in your code.

Good luck.

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • +1: An answer the use can _learn_ from without giving him copy-paste-able code. I'd explain the `~`, but for the rest, great answer :-) – Cerbrus Jan 31 '18 at 13:26
  • @Cerbrus Yeah, my comment had `if (["kiwi","banana"].indexOf(arr[x]) !=-1)` - I see that the `~` makes that shorter: https://stackoverflow.com/a/12299717/295783 – mplungjan Jan 31 '18 at 13:30
  • @mplungjan: Yea, `~-1` is `0`, which is falsy. `~0, ~1, etc` is `-1` or lower, which is all truthy. – Cerbrus Jan 31 '18 at 13:32
  • I thought that might have been beyond the scope of the question but I gave a basic explanation anyway. – I wrestled a bear once. Jan 31 '18 at 13:33
0

try this

var arr = ["apple", "orange", "banana", "kiwi"];

for(var i = 0; i < arr.length; i++) {
  if(arr[i] === "kiwi" || arr[i] === "banana") {
    arr[i] = arr[i] + "1";
  }
}

console.log(arr);

a couple of errors, first neither x or i has been declared, then you forgot a quotation in the if condition, and last you weren't really comparing the current array value to banana, that's all folks.

zola
  • 5,737
  • 8
  • 33
  • 48
0

You made a bunch of errors:

1: arr[x] === kiwi" theres a missing ". kiwi" is supposed to be "kiwi" because it is a string.

2: arr[x] === kiwi" || "banana" the problem here is || "banana" which will always return true, it should be arr[x] === "kiwi" || arr[x] === "banana"

3: for(var x= 1; x < arr.length; x++) this does not reflect but var x= 1 will skip the first item in the array so it should be var x= 0

Updated snippet:

var arr = ["apple", "orange", "banana", "kiwi"];
for(var x= 0; x < arr.length; x++) {
  if(arr[x] === "kiwi" || arr[x] === "banana") {
    arr[x] = arr[x] + "1";
  }
}

console.log(JSON.stringify(arr))
Ayo K
  • 1,719
  • 2
  • 22
  • 34