0

I’m trying to solve a tricky problem here. Whenever there are 3 rows together (e.g. 3A, 3B & 3C), I want to increment the variable ‘count’. My logic only works for the first row but then fails for the rest. The answer to this problem should be 3. Can someone shed some light here?

function doThis() {
    var everySeat = [1A, 1B, 1C, 2A, 2C, 3A, 3B, 3C, 151A, 151B, 151C, 152B, 152C];
    var count;

    for (i = 1; i <= everySeat.length; i++) {
        if (everySeat[i-1] = i + ‘A’ && everySeat[i] = i + ‘B’ && everySeat[i+1] = i + ‘C’) {
            count++;
        }
    }

}

doThis();
5
  • 3
    you should use comparison operator == in if condition Commented Oct 11, 2017 at 10:22
  • 1
    And an array that isn't a syntax error ? Commented Oct 11, 2017 at 10:23
  • I would use two dimensions array for seat[A,B,C][Number] example: everySeat[A][1,2,3,4,...] everySeat[B][1,2,3,4,...] Less mess ;) Commented Oct 11, 2017 at 10:25
  • 1
    You have items in your array, the 151 and 152 values that will never match the condition of your loop Commented Oct 11, 2017 at 10:28
  • Logging your values as you go along would help you understand what is going on. Your first condition matches because your loop value matches 1. When you get to your later values like 3 that your are trying to match, your i is now 6 so youre comparing 3A (everySeat[i-1]) to 6A (i+"A") Commented Oct 11, 2017 at 10:36

5 Answers 5

1

Try this. Works fine.

function doThis() {
    var everySeat = ['1A', '1B', '1C', '2A', '2C', '3A', '3B', '3C', '151A', '151B', '151C', '152B', '152C'];
    var count = 0;

    for (var i = 1; i <= everySeat.length-2; i++) {
        var prefix = parseInt(everySeat[i-1]);
        if (everySeat[i-1] == prefix + 'A' && everySeat[i] == prefix + 'B' && everySeat[i+1] == prefix + 'C') {
            count++;
        }
    }
return count;
}
Sign up to request clarification or add additional context in comments.

1 Comment

on second thought, would it not be for (var i = 1; i <= everySeat.length-1; i++) { as just everySeat.length? otherwise it would be 1 iteration out
0

Extended solution with Array.slice() and String.match() functions:

var everySeat = ['1A', '1B', '1C', '2A', '2C', '3A', '3B', '3C', '151A', '151B', '151C', '152B', '152C'],
    count = 0;
	
for (var i=0, l=everySeat.length; i < l-2; i++) {
    if (String(everySeat.slice(i,i+3)).match(/\d+A,\d+B,\d+C/)) {
        count++;
        i +=2;
    }	
}

console.log(count);

Comments

0

You can convert it in string then match required substring occurrence.

var everySeat = ['1A', '1B', '1C', '2A', '2C', '3A', '3B', '3C', '151A', '151B', '151C', '152B', '152C'];
var str = everySeat.toString();
var count1 = (str.match(/3A,3B,3C/g) || []).length;
console.log(count1);

1 Comment

Thats only works if he knows he is looking to match 3A, B, and C and does not solve the question. The answer should be 3
0

Firstly you array has syntax error. SO try below code.

var everySeat = ['1A', '1B', '1C', '2A', '2C', '3A', '3B', '3C', '151A', '151B', '151C', '152B', '152C'];
    var count =0;
    for(var i =0; i < everySeat.length ; i++ ){
    if(everySeat[i] == i + 'A' && everySeat[i+1] == i + 'B' && everySeat[i+2] == i + 'C'  ){
    count++;
    }
    }
    console.log(count);

1 Comment

I would also add that the for loop should go till everySeat.length-2, as it stands, you'll be trying to access non-existing elements
0

You could take a hash table for counting rows and add one if count is three for a row.

This proposal works for unsorted data as well.

var array = ['1A', '1B', '1C', '2A', '2C', '3A', '3B', '3C', '151A', '151B', '151C', '152B', '152C'],
    count = Object.create(null),
    result = array.reduce(function (r, s) {
        var key = s.match(/\d+/)[0];
        count[key] = (count[key] || 0) + 1;
        return r + (count[key] === 3);
    }, 0);

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.