# Shift operator whithin for loop

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())
``````

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

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.