1

I want to see if the index number of the array item is even, if it is then it should carry out a block of code;

var num = 122345643345673;
var convNum = num.toString();
var aftertoString = convNum.split(" ");
function luhnsAlg() {
for (let i = 0; i < aftertoString.length; i++) {
    if (aftertoString[i] == 2) {
        console.log("wow");
    }else{
        console.log("ohh");
    }
  }
}
Samuel Emeka
  • 73
  • 1
  • 9
  • 1
    Do you mean the index, or do you mean the actual digit at that index? It would be unusual to check the index when you could simply guarantee to iterate over the even indices in your for loop with `i += 2` instead of `i++`. – jarmod May 27 '22 at 14:00
  • Does https://stackoverflow.com/questions/16505559/how-can-i-use-modulo-operator-in-javascript answer your questionß – Reporter May 27 '22 at 14:01
  • 1
    for your information the % (modulo) operator will tell you the reminder of a division.. if you divide by 2 and there's no reminder (==0), the number is even otherwise odd. – Diego D May 27 '22 at 14:01
  • 1
    Also you need to correctly split the string, it should be `convNum.split("")`. You even don't need to split you can loop over the string as well. – SSM May 27 '22 at 14:03
  • 1
    @jarmod i mean the index not the digit – Samuel Emeka May 27 '22 at 14:08

1 Answers1

1

You can do :

let even = index % 2 == 0;

it will return true when even

Lk77
  • 2,203
  • 1
  • 10
  • 15
  • Please do not answer questions that have already been answered. – MrUpsidown May 27 '22 at 14:03
  • i'm sorry but i don't agree with that, OP has right to have an answer, even a simple and already answered one, it's impolite to throw a link at him. Most of the questions on stackoverflow have already been answered anyway – Lk77 May 27 '22 at 14:05
  • 1
    @Lk77 this should be for the digit i want to find out if the index number is even – Samuel Emeka May 27 '22 at 14:10
  • That is exactly why you have the "Duplicate" close vote. – MrUpsidown May 27 '22 at 14:11
  • i don't close questions, for me it's offensive – Lk77 May 27 '22 at 14:11
  • @SamuelEmeka well it's the same thing, "i" is the "index" in your case, it's the position of the letter within the string – Lk77 May 27 '22 at 14:17
  • I changed the "==" sign to "%" and still, nothing happens in the console – Samuel Emeka May 27 '22 at 14:41
  • do you execute your luhnsAlg function somewhere ? – Lk77 May 27 '22 at 14:44
  • I use code runner – Samuel Emeka May 27 '22 at 14:51
  • I just tried calling the function with a button, but the condition is not met – Samuel Emeka May 27 '22 at 14:54
  • i have ohh in the console , and for me it's the expected behaviour, since there is only one entry in the array aka : ['122345643345673'], i think you want so split each char in the string, so look at that answer : https://stackoverflow.com/a/6484689/8126784, i edited your code : https://pastebin.com/iA1ByWH8 – Lk77 May 27 '22 at 14:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245103/discussion-between-samuel-emeka-and-lk77). – Samuel Emeka May 27 '22 at 15:07