What does: var next=++current % GUINEAPATHS.length; work? At first current is 1 why is it dividing 1 by 4 the length of Path . How does it work? Why is ++ infornt of current and not after? Thank you


#1


var GUINEAPATHS = [
'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-guineapig-1.jpg',
'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-guineapig-2.jpg',
'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-guineapig-3.jpg',
'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-guineapig-4.jpg'
];

var GuineaPigsContainer = React.createClass({
getInitialState: function () {
return { currentGP: 0 };
},

nextGP: function () {
var current = this.state.currentGP;
var next = ++current % GUINEAPATHS.length;
this.setState({ currentGP: next });
},

https://www.codecademy.com/en/courses/react-102/lessons/container-presentational-components/exercises/container-presentational-components-apply?action=resume


Replace this line with your code.


#2

@systemsolver09910

// define a VARIABLE =GUINEAPATHS= with associated Array.
// with GUINEAPATHS.length you have the Array-length which is 4
// Array-index count starts with zero 0
// GUINEAPATHS[0] points to ...photo...1
// GUINEAPATHS[3] points to ...photo...4
//
var GUINEAPATHS = [
 'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-guineapig-1.jpg',
 'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-guineapig-2.jpg',
 'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-guineapig-3.jpg',
 'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-guineapig-4.jpg'
];

var GuineaPigsContainer = React.createClass({
   getInitialState: function () {
            return { currentGP: 0 };
   },

   nextGP: function () {
             var current = this.state.currentGP;
             var next = ++current % GUINEAPATHS.length;
             this.setState({ currentGP: next });
   },
   //here comes another METHOD
} //closing the PARAMETER which is an OBJECT !!!
);  //closing parentheses of the =createClass=

var next = ++current % GUINEAPATHS.length;
   // ++current is using the INCREMENT operator as PREFIX
   // % is the MODULO operator or in Javascript called REMAINDER operator

http://stackoverflow.com/questions/971312/why-avoid-increment-and-decrement-operators-in-javascript

http://stackoverflow.com/questions/6020701/javascript-increments

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence%24revision/514799#Table

% modulo operator

Javascript has the so-called Remainder-operator %
is actually a modulo-operator
and if used it will return the rest-value.

9%3 You read it as nine modulo three is zero, as 9 divided by 3 has NO rest-value.
22%6 You read 22 modulo six is 4, as 22 divided by 6 will leave you with rest-value 4
10%5 You read 10 nodulo five is zero, as 10 divided by 5 will have NO rest-value

1%4 You read it as one modulo four is one, as 1 divided by 4 has rest-value 1.
2%4 You read it as two modulo four is two, as 2 divided by 4 has rest-value 2.
3%4 You read it as three modulo four is three, as 3 divided by 4 has rest-value 3.
4%4 You read it as four modulo four is zero, as 4 divided by 4 has NO rest-value.

You will also check the so-called operator-precedence-table
and you will see that the INCREMENT operator has an =higher-precedence= (3)
as the MODULO operator (5)

Thus if you write
++current % GUINEAPATHS.length;
the =current= VARIABLE will be INCREMENTED with 1
BEFORE
the MODULO operator is evaluated.


#3

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