King King - 5 months ago 5
Javascript Question

Replace an element in an array

Need to replace a value "y" with "#" in an array and also need to count the "y" value in an array.

x = ['a','b','c'];
z = ['z','y','y'];

var a = x.concat(z);
var b = a.sort();
var count = 0;
for(var i = 0; i < b.length; i++){
if(b[i] == "y") {
b['y'] == "#";
var c = count++;
}
}
console.log(b);
console.log(c+1);


Fiddle

Answer

== is comparison operator = is assignment operator

Change to this :

for(var i = 0; i < b.length; i++){
    if(b[i] == "y") {

b[i] = "#";

        var c = count++;
    }
}

from

for(var i = 0; i < b.length; i++){
    if(b[i] == "y") {

b['y'] == "#";

        count++;
    }
}

x = ['a', 'b', 'c'];
z = ['z', 'y', 'y'];

var a = x.concat(z);
var b = a.sort();
var count = 0;
for (var i = 0; i < b.length; i++) {
  if (b[i] == "y") {
    b[i] = "#";
    count++;
  }
}
console.log(b);
console.log(count);