 # Reading Assignment – Objects and Arrays

If after searching in Google, some questions are still troubling you with ‘Data Structures: Objects and Arrays’ section in the book, please post your questions below

What does the 5th line mean with the base is multiplying by a function? Previously I read in the book “eloquent javascript” that you can store a function with the factor and call the function with the stored factor with an argument. But how does this program work by multiplying returned values? How?

``````function power(base, exponent) {
if (exponent == 0) {
return 1;
} else {
return base * power(base, exponent - 1);
}
}

console.log(power(2, 3));
``````

What does `"(\${history} + 5)"` mean on line 8? Is this better than `history += " + 5"` ? Because I’m more comfortable with this and don’t completely understand the first one. Are there more possibilities with this or is the 2nd completely fine?

``````function findSolution(target) {
function find(current, history) {
if (current == target) {
return history;
} else if (current > target) {
return null;
} else {
return find(current + 5, `(\${history} + 5)`) ||
find(current * 3, `(\${history} * 3)`);
}
}
return find(1, "1");
}

console.log(findSolution(24));
// → (((1 * 3) + 5) * 3)
``````

In the 5th line, the power function returns base and keep calling the function while subtracting the exponent value by 1, until it reaches 0, and it return 1 and stops.
so the process for (power(2,3)) look like this:
power(2,3) (Calling power(2,3))
2* (Calling power(2,2)
2*2 (CALLING power(2,1)
2*2*2*1 (CALLING power(2,0)

All assignments for the chapter “Data structures: Objects and Arrays” already include functions. They include functions because functions have been explained in the previous chapter of the book. Whereas, in this course, arrays are coming later, 3 or 4 videos later after this lesson. So, my question is, are we supposed to study arrays by ourselves?

``````function fib( n ) {
let fibCurrent = 1;
let fibLast = 0;
if ( n < 0 ) return NaN;
if ( n <= 1 ) return n;
for ( let fibIndex = 1; fibIndex < n; ++fibIndex ) {
[fibCurrent, fibLast] = [fibCurrent + fibLast, fibCurrent];
}
return fibCurrent;
}
``````

I do not understand why it is the solution? (line 7) What line 7 is supposed to do?
Thanks

That code of line is updating the `fibCurrent` and `fibLast` at the same time, this method called destructuring assignment, You can read here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment.

your answer is not helping. I have read about restructuring, including Mozilla. I had asked you to help me to understand it. Having read everything, I still do not understand why this is the solution. Thank you.

Hey @pletniri, hope you are well.

`[fibCurrent, fibLast] = [fibCurrent + fibLast, fibCurrent];`

Just like @Maki said:

Example:

``````let fibCurrent = 1;
let fibLast = 2;

[fibCurrent, figLast] = [fibCurrent + fibLast, fibCurrent];

console.log(fibCurrent , fibLast ); // 3 2
``````

Carlos Z

I checked the Eloquent JS solution to the range exercise in chapter 4, and didn’t understand, so I googled to find other solutions (I did this because I was wracking my brain trying to figure out the solution)!

I tried to go through this new solution below, step-by-step, and I understand most of it apart from the line below the commented section. inc means increment btw and cur means current.

``````      <script>
function range(s, e, inc) {
var array = [];
var cur = s;
// What does this bit mean?
inc = inc || 1;

if (inc > 0) {
while (cur <= e) {
array.push(cur);
cur += inc;
}
}
else {
while (cur >= e) {
array.push(cur);
cur += inc;
}
}
return array;
}
console.log(range(2, 9, 2));
//[2, 4, 6, 8];
</script>
``````
1 Like

There is one thing about a destructuring exercise from the es6 in practice book that I don’t understand.
It is the comma in the destructured answer code.
Why is it that fibCurrent is displayed again after a comma?
What does this comma (on the right of the = sign) mean for the output?

``````• fib( 0 ) = 0
• fib( 1 ) = 1
• fib( n ) = fib( n-1 ) + fib( n-2 );
function fib( n ) {
let fibCurrent = 1;
let fibLast = 0;
if ( n < 0 ) return NaN;
if ( n <= 1 ) return n;
for ( let fibIndex = 1; fibIndex < n; ++fibIndex ) {
// Insert one destructuring expression here
}
return fibCurrent;
}
``````

``````function fib( n ) {
let fibCurrent = 1;
let fibLast = 0;
if ( n < 0 ) return NaN;
if ( n <= 1 ) return n;
for ( let fibIndex = 1; fibIndex < n; ++fibIndex ) {
[fibCurrent, fibLast] = [fibCurrent + fibLast, fibCurrent];
}
return fibCurrent;
}

``````

The Fibonacci sequence is the series of numbers where each number is the sum of the two preceding numbers. 1+1=2,1+2=3,2+3=5,3+5=8,5+8=13 and so forth.

``````function fib(n) {
let fibCurrent = 1;
let fibLast = 0;
if (n < 0) return NaN;
if (n <= 1) return n;
for (let fibIndex = 1; fibIndex < n; ++fibIndex) {
[fibCurrent, fibLast] = [fibCurrent + fibLast, fibCurrent];
console.log("fibCurrent", fibCurrent);
console.log("fibLast", fibLast);
}
return fibCurrent;
}
``````
``````console results will be fibCurrent 1
fibLast 1
fibCurrent 2
fibLast 1
fibCurrent 3
fibLast 2
fibCurrent 5
fibLast 3
``````
``````[fibCurrent, fibLast] = [fibCurrent + fibLast, fibCurrent];  // Here we are updating the fibCurrent and fibLast values at the same time. You can also replace that code of line with
fibCurrent = fibCurrent + fibLast;
fibLast = fibCurrent - fibLast; It will work the same but here with no destruction.
``````
1 Like

i just want to ask why this chapter is very very confusing i have been reading the eloquent javascript and started to understand it, but when i tried doing es6 exercise, somehow i feel it’s like a huge jump cos I couldn’t understand anything. I also use google & watch a lot of explanation video in youtube, but i still don’t really get it. do you have any advice?

1 Like

If a chapter is tough for you, continuously repeat the basics, e.g. basic types.

``````[fibCurrent, fibLast] = [fibCurrent + fibLast, fibCurrent];
``````

is the same as writing:

``````let temp = fibCurrent;
fibCurrent = fibCurrent + fibLast;
fibLast = temp;
``````

because in assignments of form [left1, left2] = [right1, right2], left1 = right1 AND left2 = right2 take place simultaneously.

Hello I’m having trouble calling the function of fib(n) to see if this function is working correctly.

``````function fib( n ) {
let fibCurrent = 1;
let fibLast = 0;
if ( n < 0 ) return NaN;
if ( n <= 1 ) return n;
for ( let fibIndex = 1; fibIndex < n; ++fibIndex ) {
// Insert one destructuring expression here
[fibCurrent,fibLast] = [fibCurrent + fibLast,fibCurrent];
console.log("fibCurrent", fibCurrent);
console.log("fibLast", fibLast);
return fibCurrent;
}
}
console.log(fib(5));
``````

Any number I put into console.log(fib(5 or 3 or 10) the output is always the same.

``````fibCurrent 1
fibLast 1
1
``````

Not sure what I’m doing wrong I was trying to replicate your console answers.

The issue is because the return is inside the for loop block So it is returning the values after on round, if you pace outside it, it will solve it. like this.

``````function fib(n) {
let fibCurrent = 1;
let fibLast = 0;
if (n < 0) return NaN;
if (n <= 1) return n;
for (let fibIndex = 1; fibIndex < n; ++fibIndex) {
[fibCurrent, fibLast] = [fibCurrent + fibLast, fibCurrent];
console.log("fibCurrent", fibCurrent);
console.log("fibLast", fibLast);
}
return fibCurrent;
}

console.log(fib(5));
``````
2 Likes

Hello everybody. Thanks for putting this Javascript course together.

I have no background in coding or IT. I understand mostly the videos. But I’m not able to solve any exercises. They all seem so abstract. Is this normal for a complete beginner?

1 Like

@cyl2chan it can be hard at the start. but keep pushing through and trying everyday you will get there eventually. so i wouldnt worry about this many pplar ein the same boat

1 Like

Thanks for the reply! After not knowing how to solve the problems or questions, I just go on to read the answers. Then I’ve found out the answers aren’t something I would be able to come up with. I think I should move onto the next videos then.

1 Like

yes dont worry i you cant. many ppl are the same and after all this is what practice is for!!

1 Like