Declare JavaScript Objects as Variables

Declare JavaScript Objects as Variables
0

#1

Tell us what’s happening:
What happens if you set the first one to 3? and if you set the second one to zero?

Your code so far

var fruits = ['apple', 'pear', 'mango', 'orange', 'banana'];

// create an empty list for fruits ending with 'e'
var efruits = [];
for (var i=0;i<fruits.length;i++){
    var fruit = fruits[i];
    //check if the current fruit end with 'e'    
    if (fruit.endsWith('e')){
        // add fruit ending with 'e' to the list
        efruits.push(fruit);
    }
}

var multiplesOfThee =0;
for (var i=1;i<=300;i++){
    if (i % 3 === 0){
        multiplesOfThee -= '4'//i; 
      console.log(multiplesOfThee);
    }
}
//console.log('4');

var motorBike = {

  // Only change code below this line.

};

Your browser information:

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

Link to the challenge:
my personal challenge


#2

“first one” refers to first variable, just to make sure you know.


#3

This is to ensure I know how a loop works.


#4

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#5

Hi, are you available?


#6

I’m on my phone right now, so I can’t manipulate code easily. It looks like you are just asking about what happens if you change either arguments or internal values (I wasn’t sure).

What happened when you tried it? How did it behave differently than you expected? What part doesn’t make sense?


#7

I was asking what would happen if you changed the 1 to a 3 (in the one about doing 300 iterations), would that start on the third iteration?
The iterations would look like this:
before it runs
first 3
second 9 (3+6)
third 15 (this would be 6+9) —this is where it would start?

Does that sound correct?


#8

If you change the 1 to a 3 in the following code, it still starts on the first iteration. When ever a loop starts in general, the first time it runs, is the first iteration.

var multiplesOfThee =0;
for (var i=3;i<=300;i++){
   if (i % 3 === 0){
      multiplesOfThee -= '4'//i; 
      console.log(multiplesOfThee);
   }
}

May I ask what you are trying to accomplish with the above code? When you write console.log(multiplesOfThee), what exactly are you hoping to display to the console? Are you trying to get a count all the number between 1 and 300 which are multiples of 3?


#9

Why ask other people what would happen instead of just changing it and seeing what happens?


#10

Because I’m confirming what I see. Sometimes I can atttribute causal factors that are actually not causal factors. At this stage, for me to understand everything else later on, I need to make sure that I’m on the right trajectory.


#11

Try it. See what happens. See if you understand why it happens. If you’re not sure that you understand what is happening or why, come and discuss it with us.


#12

This question is just making sure I understand whats going on but I found that the += iterator makes it add on to the previous one. Does the ± one make it subtract in this way:
-3
-9 what it is considering:-3-6
18 what it is considering:-6-9

etc.

Im trying to understand iterators a little better


#13

Sorry, I was only early this morning, but then had a dentist appointment and a 10 mile run.

The += operator adds a value to a variable. The following two lines of code achieve the same thing.

var a = 5;
a = a + 10; // a = 10

var b = 5;
b += 10; // b = 10

The -= operator subtracts a value from a variable. The following two lines of code achieve the same thing.

var a = 20;
a = a - 5; // a = 15

var b = 20;
b -= 5; // b = 15

In your question above, you referred to a ± operator, but there is no such operator in JavaScript.


#14

That wasn’t my intention. I meant to refer to the += and the -= variables and specifically what they do in a for loop


#15

I guess I am not understanding what you are asking. The += and the -= operators would work the same as above or in a for loop. Can you show an example of code using += or -= in a for loop that you do not understand the result?


#16

var multiplesOfThreeSums = 0;
var numberCounter = 0;
for (var i=1;i<=300;i++){
if (i % 3 === 0){
numberCounter++;
multiplesOfThreeSums += i;
}
}

The above adds sums 300 times. How would I subtract 300 times from a starting number?


#17

You did not specify what the starting number should be or what value you wanted to subtract 300 times from the starting number, so I made up my own values.

var number = 2000; // starting number
var subtractThis = 5; // a number you want to subtract 300 times from the starting number

for (var i=1;i<=300;i++){
  number -= subtractThis;
}
console.log(number); // displays 500