Shift operator whithin for loop

Task: Training on Odd March Bits 8 bits | Codewars

My solution so far

function bitMarch (n) {
  	//Initial array creation
	var x = Array.from({length: 8-n}, k => 0) // eg for n = 5, x = [0, 0, 0]
    var y = Array.from({length: n}, e => 1)	// e.g for n = 5, y = [1, 1, 1, 1, 1]
    var start = x.concat(y); 
  
  result = []; temp = start.slice()
 	for(let i = n ; i <= 8; i++){
      
      //POINT OF ENQUIRY. Shouldn't this execute first, and first element of result...
     	result.push(temp)	
      //...be the first value of temp be [0, 0, 1, 1, 1 ,1, 1, 1] ?
        
      	s = temp.shift()
      	temp = temp.concat(s)
    }
  
 return result
}

Now t seems that that the for loop executed until completion BEFORE the arrays are added to the result? bitMarch(6) returns [[0,1,1,1,1,1,1],[1,1,1,1,1,1,0],[1,1,1,1,1,0,0]] . How can I fix the for loop so that the temp is added THEN shifted, rather than having what seems to be the shift happening first?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

That is odd behavior. I think that what is happening is this:

      result.push(temp)
      s = temp.shift()

You are calling push with that reference. While it is evaluating it, the shift takes place, so by the time the push reads that data, at that reference, the data has been updated. That is odd. I’ve seen that behavior before with things like console.log. I would not expect it to happen with something like push which I would expect to be completely synchronous. Perhaps there is some kind of optimization since you are doing an operation on a different array, so it doesn’t think it needs to wait. Maybe someone smarter than me will log on and offer an explanation.

In any case, it works if I send it a copy, so it is getting a different reference, whose data won’t be altered.

      result.push(temp.slice())

It’s all about references.

First you push a reference to the temp array (bad variable name).

Then you modify the temp array with shift, which is seen in the reference.

Then you replace temp with a new arry via concat.

1 Like

Ah yes, I must be tired. You’re pushing the reference, not the data.

1 Like

Thanks. Why is temp a bad variable name?

A variable should tell you what it is. For a variable holding data, it should usually be based around a noun. Yours is the abbreviation of an adjective. The name should tell me what it is - I shouldn’t have to read the code to figure it out. There are a few exceptions, like i, j, or k as looping variables or one letter variables in simple arrow function callbacks, but in general, naming things well is very important. And in all fairness sometimes it is difficult. It’s not uncommon for me and my coworkers to hop on Slack and ask for help with a name.

2 Likes