2

Is it allowed to do something like Array[i - 1] in javascript? Assuming that i and i-1 are proper indexes of the Array.

I am wondering because it seems to give me errors but I don't know if the error is how I refer to indexes.

Furthermore, is it possible to do something like Array[Array.length - 1]?

The actual error is with this:

input = input.split('\n');
    for (var i = 1; i < input.length; i++){
        for (var j = 0; j < input[i].length; j++){
            process.stdout.write(input[i][j]);
            process.stdout.write(input[i][j - 1]);
            process.stdout.write(input[i][input[i].length - 1 - j]);
            process.stdout.write(input[i][input[i].length - j]);
          if (input[i][j] - input[i][j - 1] !== input[i][input[i].length - 1 - j] - input[i][input[i].length - j]){

          }  
        }

    }

Of the process.stdout.write statements, only the first outputs anything.

6
  • 2
    Yes. You can put any sort of expression you want inside [ ] so long as it evaluates to a number or a string. Post your actual code and the actual errors you're getting. Commented Oct 7, 2015 at 16:22
  • 1
    What "errors" are you getting? You can put any expression you want inside the []. Commented Oct 7, 2015 at 16:22
  • 1
    Yes, it's possible to use a.length - 1 as index to the array, in fact it is used to get the last element from array Commented Oct 7, 2015 at 16:24
  • Try avoiding plain yes/no questions. Add your code and errors to make your question more valuable. Commented Oct 7, 2015 at 16:35
  • Updated the question, my bad. Commented Oct 7, 2015 at 16:40

1 Answer 1

2

Yes, it`s possible.

See here: https://jsfiddle.net/hca4cecc/

var myObj = [1, 2, 3, 4, 5];
alert(myObj[2-1+3]);

Edit: With your updated information: There is a bug. The [j-1] is -1 in the first loop and your code should crash.

Sign up to request clarification or add additional context in comments.

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.