dadadodo
dadadodo

Reputation: 347

Comparing array elements with each other

I have a char array:

var charCode = [97,98,100,101,103];

I want to loop through this array and compare charCode[i] with charCode[i+1] so compare:

charCode[0] with charCode[1]
charCode[1] with charCode[2]
charCode[2] with charCode[3]
charCode[3] with charCode[4]

I also want to check if charCode[i] +1 == charCode[i+1] so this means i want to see if the next element is bigger than last by factor on one. One more thing, if e.g charCode[3] does not follow that rule I want to store the element's index in a separate variable.

function fearNotLetter(str) {
  var bool ;
  var charCode = [];
  for (var i = 0; i < str.length; i++) {
    charCode[i] = str.charCodeAt(i);
    // charCode = [97,98,99,100,101,103]
  }
  for (var n = 0; n < charCode.length; n++) {
    /*
    here I'm comparing every element with every other element, 
    which is obviously not what I want
    */
    for (var j = n+1; j < charCode.length; j++) {
      if (charCode[n] + 1 < charCode[j]) {
        // don't know what to do here
      }
    }
  }
  return  charCode;
}
fearNotLetter("abcdeg");

Upvotes: 0

Views: 139

Answers (4)

dmychu
dmychu

Reputation: 1

Do you want to get the failed indexes?

function fearNotLetter(str) {
	var failedElIndexes = [];
	var j = 0;
	for(var i = 0 in str){
		if(i < (str.length-1)){
			var n = str[i];
			var m = str[++i];
			if( String.fromCharCode(n.charCodeAt() + 1 ) != String.fromCharCode(m.charCodeAt() ) ) {
				console.log(i + ": " + str[i]);
				failedElIndexes[j] = i;
				j++;
			}	
		}else{
			break;
		}

	}
	return failedElIndexes;
}

var otherStr = "abcdeg";
var myErrIndexes = fearNotLetter(otherStr);

Upvotes: 0

Rajesh
Rajesh

Reputation: 24915

You can use array.reduce and on mismatch, you can push that value to an intermediate array

function fearNotLetter(str) {
  var charCode = [];
  str.split('').reduce(function(p,c){
    var code_p = p.charCodeAt()
    var code_c = c.charCodeAt()
    if(code_c-code_p !== 1)
      charCode.push(code_p)
    return c
  })  
  return  charCode;
}

console.log(fearNotLetter('abcdeg'))

Upvotes: 1

synthet1c
synthet1c

Reputation: 6282

Here is a couple of modifications. You were running the inner loop where you did not need to.

function fearNotLetter(str) {
  var bool ; // not sure what this is for
  // you can borrow map from array instead of looping
  var charCode = Array.prototype.map.call(str, str => str.charCodeAt(0))
  // loop over the characters. I am starting at 1 so we stop when we get to the end
  // you can access the first index by subtracting 1 from ii
  for (var ii = 1; ii < charCode.length; ii++) {
    if (charCode[ii - 1] === charCode[ii]) {
      console.log('samsies', ii - 1, char(charCode[ii]), char(charCode[ii]))
    }
    else if (charCode[ii-1] + 1 === charCode[ii]) {
      console.log('increment yo', ii - 1, char(charCode[ii -1]), char(charCode[ii]))
    }
  }
  return  charCode;
}
console.log(
  fearNotLetter("abcddeg")
)
// helper function
function char(code) {
  return String.fromCharCode(code)
}

Upvotes: 0

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21565

You just need a normal loop, just don't go to the last item since you are checking [i+1]:

var charCode = [97,98,100,101,103];
// Loop from 0 to n-2.
for (var i = 0; i < charCode.length - 1; i++) {
  if(charCode[i] +1 == charCode[i+1]) 
    console.log(charCode[i] +1 + '==' + charCode[i+1]);
}

Upvotes: 0

Related Questions