user12979978
user12979978

Reputation:

Can anyone tell me why the value of the variable "item" is UNDEFINED?

*So I'm trying to manipulate the string at a specific index. I have to store the element at a specific index to the "item" variable. But it somehow has the value "undefined". Can anyone tell me what's happening ? Thanks !! *

"use strict";

const shinigami = function (string) {
  //   console.log(string);
  const array = string.split("");
  //   console.log(array);
  for (var i in array) {
    if (array[i] === "M") {
      console.log(array.indexOf("M"));
      array.splice(array.indexOf("M") - 1, 1);
      array.splice(array.indexOf("M") - 1, 1);
    } else if (array[i] === "N") {
      const item = array[i + 1];
      console.log(item); // Console:  Value of item is UNDEFINED !!
    }
  }
  console.log(array);
};

const string = "abcdNefgh";
shinigami(string);

Upvotes: 0

Views: 48

Answers (4)

kstepien
kstepien

Reputation: 1233

Because your value of i is a string. And if you add number into string like this: "abc" + 1 is "abc1" also if you add "1"+2 is "12" not 3.

"use strict";

const shinigami = function (string) {
  //   console.log(string);
  const array = string.split("");
  //   console.log(array);
  for (var i in array) {
    if (array[i] === "M") {
      //console.log(array.indexOf("M"));
      array.splice(array.indexOf("M") - 1, 1);
      array.splice(array.indexOf("M") - 1, 1);
    } else if (array[i] === "N") {
      const item = array[i + 1];
      console.log(array, array[i], array[i + 1], i, i+1);
    }
  }
  //console.log(array);
};

const string = "abcdNefgh";
shinigami(string);

If you want to fix it you can convert i into a number. Like this:

"use strict";

const shinigami = function (string) {
  const array = string.split("");

  for (const i in array) {

    if (array[i] === "M") {

      array.splice(array.indexOf("M") - 1, 1);
      array.splice(array.indexOf("M") - 1, 1);
    } else if (array[i] === "N") {
      const item = array[~~i + 1];
      console.log(item);
    }
  }
};

const string = "abcdNefgh";
shinigami(string);

Upvotes: 2

Ahmed Sunny
Ahmed Sunny

Reputation: 2243

because its doing concatenation instead of add

you can do this way, there are more ways

const item = array[eval(i) + eval(1)]; 

or use ++i

Upvotes: -1

vanowm
vanowm

Reputation: 10201

It's because i contains a string, not a number, so by adding 1 to it you create 41 not 5

Convert it to number with array[~~i + 1]

Upvotes: 2

Weagertron
Weagertron

Reputation: 191

It seems there are some issues with the const item = array[i + 1] line. If you do a console.log on something like i + 5 it will become a string value.

This code seems to work when replacing the loop with a for loop with a defined number index:

"use strict";

const shinigami = function (string) {
  //   console.log(string);
  const array = string.split("");
  //   console.log(array);
  for (let i = 0; i < array.length; i++) {
    if (array[i] === "M") {
      console.log(array.indexOf("M"));
      array.splice(array.indexOf("M") - 1, 1);
      array.splice(array.indexOf("M") - 1, 1);
    } else if (array[i] === "N") {
      const item = array[i + 1];
      console.log(item); // Console:  Value of item is not UNDEFINED !!
    }
  }
  console.log(array);
};

const string = "abcdNefgh";
shinigami(string);

Upvotes: 1

Related Questions