-1

I have a integer array

array = [1,1,1,1,0,0,0,0]

that I want to convert to a string array based on the following logic:

for(var index in arr){
   if(arr[index] == 0){
      arr[index] == 'Closed'
   }
   else{
      arr[index] == 'Open'
   }
}

In order to get the following output

arr = ['open','open','open','open','closed','closed','closed','closed']

But the code is not executing correctly. Can you not assign strings to arrays in Javascript? Any help would be appreciated.

William Merritt
  • 429
  • 1
  • 5
  • 12
  • 9
    This may be the first time I've seen `==` confused for `=` and not vice-versa. Anyway assignment (setting a value) is `=`, comparison is `==` (or `===`). – Pointy Apr 09 '18 at 22:21

4 Answers4

3

You have a typo

arr[index] == 'Closed'
           ^

And here

arr[index] == 'Open'
           ^

There you're making a comparison rather than an assignation.

Another alternative to accomplish your requirement is using the function map:

This approach will create a new array

const array = [1,1,1,1,0,0,0,0],
      result = array.map(n => n === 0 ? 'closed' : 'open');
      
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

A little shorter using coercion from boolean (1 = true, 0 = false)

const array = [1,1,1,1,0,0,0,0],
      result = array.map(n => n ? 'open' : 'closed');
      
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Ele
  • 33,468
  • 7
  • 37
  • 75
1

You are using comparison operations rather than assigning the value, it should be arr[index] = 'Open' rather than arr[index] == 'Open'

You can use Array.map to test the value of the array items. The return value is a new array of the updated value

es5 example

var converted = arr.map(function(value){ if (value ==0) { return 'closed'; } return 'open'; });

es6

const converted = arr.map(value => value === 0 ? 'closed' : 'open');

Stephen
  • 3,607
  • 1
  • 27
  • 30
  • 1
    The thing is, those are **not** syntax errors. They're *syntactically* fine, and there'll be no syntax errors reported in the developer console. They are, of course, *semantically* incorrect. – Pointy Apr 09 '18 at 22:36
  • 1
    You are right, there isn't anything wrong with the syntax. It just doesn't achieve what was desired. There wouldn't be any errors so my comment isn't correct – Stephen Apr 09 '18 at 22:38
  • JavaScript challenges us :) – Pointy Apr 09 '18 at 22:40
0

you can use map();

array = [1,1,1,1,0,0,0,0];
const result = array.map(el => el === 1 ? "open" : "close");
console.log(result);
scraaappy
  • 2,830
  • 2
  • 19
  • 29
  • This is fine, but (almost) code-only answers don't provide a lot of insight and clarity to new-to-JavaScript developers. It'd be good if you added a description of how `.map()` works, how that fat-arrow function works, etc. – Pointy Apr 09 '18 at 22:24
0

You are using == rather than = in some places. Double equals is used for comparison, like checking if a cup has orange juice in it. Single equals is used for setting a variable, like pouring orange juice into the cup. The correct code should be:

for(var index in arr) {
    if(arr[index] == 0) {
        arr[index] = 'Closed'
    } else {
        arr[index] = 'Open'
    }
}

Another thing to consider is that it is generally bad practice to change the type of an array, even it is possible in JavaScript. The reason is that it is then difficult if, later on in your code, you want to get the original binary array back.

Best wishes, Luke.