Learn Introductory JavaScript by Building a Pyramid Generator - Step 99

Tell us what’s happening:

On Step 99, I wrote:
const numbers = [1, 2, 3];

const unshifted = numbers.unshift(5);
console.log(unshifted);

const shifted = numbers.shift();
console.log(shifted);

The systemon the console showed 4 (the correct answer for unshifted) and 5(right answer for shifted) but said:
“You should call .shift() on your numbers array.
You should assign the result of your .shift() call to your shifted variable.”
Of course I had done that… Color me confused, please

Your code so far

const character = "#";
const count = 8;
const rows = [];

function padRow(rowNumber, rowCount) {
  return " ".repeat(rowCount - rowNumber) + character.repeat(2 * rowNumber - 1) + " ".repeat(rowCount - rowNumber);
}

// TODO: use a different type of loop
/*for (let i = 1; i <= count; i++) {
  rows.push(padRow(i, count));
}*/

/*while (rows.length < count) {
  rows.push(padRow(rows.length + 1, count));
}*/

/*for (let i = count; i > 0; i--) {
  rows.push(padRow(i, count));
}*/


// User Editable Region




// User Editable Region


let result = ""

for (const row of rows) {
  result = result + "\n" + row;
}

console.log(result);

//Step 99

const numbers = [1, 2, 3];

const unshifted = numbers.unshift(5);
console.log(unshifted);

const shifted = numbers.shift();
console.log(shifted);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Learn Introductory JavaScript by Building a Pyramid Generator - Step 99

Your issue is the order.

Here was the original code

const numbers = [1, 2, 3];


const unshifted = numbers.unshift(5);
console.log(unshifted);
console.log(numbers);

You needed to write your answer in that space here

const numbers = [1, 2, 3];

// answer goes here

const unshifted = numbers.unshift(5);
console.log(unshifted);
console.log(numbers);

So why does order matter?
Because when you use these methods of push, pop, shift, etc it mutates the original array.

To better understand,
here is what the correct result produces in the console.
I added an extra console statement so you can better see the difference.

This is the original numbers array [ 1, 2, 3 ]
This is the result of the shifted variable 1
This is the updated array after shift was used [ 2, 3 ]
This is the result of unshift 3
This is the updated array after unshift was used [ 5, 2, 3 ]

Now here is your result.
Pay close attention to the difference in the arrays

this is the original numbers array [ 1, 2, 3 ]
result of unshift 4
this is the mutated array after unshift [ 5, 1, 2, 3 ]
Result of shift 5
this is the mutated array after shift [ 1, 2, 3 ]

Hope that helps

1 Like

There is a blank line, reset the lesson, you will see a blank line that where u are supposed to work on this is the solution though
const numbers = [1, 2, 3];

const shifted = numbers.shift();

console.log(shifted);

const unshifted = numbers.unshift(5);

console.log(unshifted);

console.log(numbers);

@Qudus and @Jwilkins - thank you. I think I see that my error was to do unshift first? I appreciate your help!