Exercise: loop and for

Asked

Viewed 2,258 times

3

Run a function called passandoPor printing on the console "here I have the value of x" where x will be the value of i for each iteration, for each value from 0 to 3.

The code I made was this below! The ending is wrong with all certainty and well incomplete, but in fact I do not know what to do to run!

function passandoPor () {
  for (var i=0; i <2; i++) {
    console.log ("aqui eu tenho o valor de", i)
  }
}

That is the mistake:

Your solution did not pass the tests Test results:

Print byPor() should print 'here I have the value of 0','here I have the value of 1','here I have the value of 2','here I have the value of 3'

your solution prints the following result: 'here I have the value of naqui I have the value of n' == 'here I have the value > 0 naqui I have the value of 1 naqui I have the value of 2\

  • 1

    You want to perform the function?

  • Yeah! Making a mistake!

  • What error are you getting? Click [Edit] to edit add your reply and add more details, such as the error you say is occurring.

  • All right, I edited!

  • The problem is in the iteration condition of the for, your loop iterates while i is less than 2. You have to iterate while i is less than or equal to 3. Read Instruction for

  • Ok! I’ll try! Thank you very much!

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

Show 2 more comments

6 answers

4

I do not know where the statement comes from, but it is not good, I would rethink if it is worth continuing with this source of study. For example he talk about xand have the loop variable be i. Maybe he wants you to create a variable x which is exactly the value of i, but for what? Maybe you want me to use x and not i, but it makes no sense inside the writing. And in fact a statement should not give names to variables, part of learning should be learning to name well on its own.

Well, after editing we realized that the problem was not even the statement was in what was posted here (apparently my interpretation was correct), we need to be careful with what question, ask is much easier than programming, if you make a mistake in this complicates. The site is reporting errors that could not happen in the code posted here, so maybe what was posted here is different than what posted there, it gets more complicated still help, I tried.

Despite the ambiguity I will interpret that you must print from 0 to 3, inclusive. Your code is printing from 0 to 2 exclusive (the 2 neither prints). To go to 3 you must use this number and not 2 as it was used (this is not a matter of knowing how to program), and to be inclusive and not exclusive you must use the <= (smaller or equal) and not the < (minor) (this is mathematics and not programming).

There were no other mistakes, no programming mistakes. And it’s not making any mistakes in the code, it’s just not giving the expected result and the site you’re using to fix this is denying this result, you need to understand that the code is not wrong, the algorithm is not doing the intended, it’s a misguided logic, without understanding it will have difficulties (and will have others if you fail to understand that this site is not the normal execution process of a code.

If you still continue with the error is because the wrong statement means something else, for example you should use < because 3 is exclusive.

Note that I improved some things in the code that do not give error or bad result, but it is good to keep learning.

The function flame may not be needed on the site you are using, which we don’t know what it is and so we can’t help any more than this. A thorough reading in Stack Overflow Survival Guide in English would help to ask better questions and prepare better for programming.

function passandoPor() {
    for (var i = 0; i <= 3; i++) console.log("aqui eu tenho o valor de", i);
}
passandoPor();

I put in the Github for future reference.

  • I find it interesting to put the body of for between keyed blocks instead of a block inline, to facilitate understanding of what is happening there. Notions of programming _(ツ)_/.

  • I think the opposite, I can even talk about it (what you have already done) but show that there is only one way to do it and the person understand instead of always doing it the same for me teaches more. Not a new concept but a different syntax from the exact same thing.

  • "what you’ve already done" - Where and what did I do? : P

  • 1

    You said you could use the keys too.

  • Thank you so much for all your attention.

  • The site is really complicated, it doesn’t work at all! But anyway, thank you very much for your attention!

Show 1 more comment

4

Good afternoon!

Follow the solution below, as requested.

function passandoPor() {
  for(var i = 0; i <=3; i++) {
    var x = i;
   console.log("aqui eu tenho o valor de " + x);
}
}

Any doubt I’m available!

  • Thank you!!! Helped a lot!!

2

I am doing this same exercise and it has some peculiarities at the time of correction that were not taught:

function passandoPor() {
 var frase = "aqui eu tenho o valor de ";
    for (var i = 0; i < 4; i++) {
      console.log(frase.concat(i));
  }
}
  • You helped me so much! Thanks msm!! I tried everything and nothing worked out!

2

As they said up there, I thought it was strange this X and I thing, but a really cool way to do it is to control the two variables by the for, so the code would look something like this:

function passandoPor() {
    for(var i = 0, x = 0; i <=3; i++, x++) {
     console.log("aqui eu tenho o valor de " + x);
  }
}

passandoPor()

  • It is worth noting that it still doesn’t make much sense because x and i are doing the same thing...

1

The source of these questions is a leveling site, which is prospecting candidates who crave scholarships, and there, it is necessary that the function is called exactly the way they want, even if there are several ways and perform this function. Here is the correct way to run:

function passandoPor() {
for(var i=0; i<4; i++) {
console.log(‘aqui eu tenho o valor de’ + “ “ + i);
}
}

In the tips there is information that is necessary to concatenate!

1

You must concatenate using + and no comma

function passandoPor () {
  for (var i=0; i <4; i++) {
    console.log ("aqui eu tenho o valor de" +  i)
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.