Nested for loops


#1

Hello, I need help. I am trying some code for a school project.
I have to print first n terms of this sequence: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 ...

So far, I did this:

function frSeq(n) {
  result = [];
  for(i=1;i<=n;i++){
    for(j=1; j<=i;j++) {
      result.push(j);
    }
  }
  console.log(result);
}

I know, this one is wrong, that it prints full subsequences instead of terms, so can you just point me the right direction, please?
XOXO G.


#2

you could do this:

function frSeq(n) {
  result = [];
  for(i=1;i<=n;i++){
    sub_thingie = []
    
    for(j=1; j<=i;j++) {
      sub_thingie.push(j);
    }
    result.push(sub_thingie)
  }
  console.log(result);
}
frSeq(5)

in the first loop, you create an array, you push the terms into this array, then push this array into results, and you have what you want


#3

sub_thingie is a bad name, please give it a better name


#4

thank you, but this doesn'to solve my problem ...
for frSeq(2) i need to get result [1,2], not [[1],[1,2]]


#5

and what do you need for 5 then?


#6

i need this frSeq(5)=[1,1,2,1,2], frSeq(6) = [1,1,2,1,2,3] ...


#7

please be a bit more descriptive, what way does the data need to presented? Strings? integers? arrays? different lines? same line? Please write how the output must look for a couple of sequences

don't forget, i haven't seen the assignment, so i am in the dark


#8

output should be array of integers:
for 1 → [1]
for 2 → [1,1]
for 3 → [1,1,2]
for 10 → [1,1,2,1,2,3,1,2,3,4] ... better?


#9

but then you can simply use lesser then in the second for loop:

function frSeq(n) {
  result = [];
  for(i=1;i<=n;i++){
    for(j=1; j<i;j++) {
      result.push(j);
    }
  }
  console.log(result);
}
frSeq(3)

so the value is not included


#10

yes, i have tried that, but that does not work for n = 5 (i should get [1,1,2,1,2] instead of [ 1, 1, 2, 1, 2, 3, 1, 2, 3, 4 ])


#11

ah like that, was there no better way for you to explain the desired sequence?

it is to late now, i will look into it tomorrow


#12

Sorry for that.... It was a lil' late and my english still sucks!


#13

i dislike this problem, i got a lot closer:

function frSeq(n) {
  result = [];
  counter = 1;
  do {
      for (i = 1; i < counter; i++){
          result.push(i)
      }
      counter++;
  } while (n > result.length)
  result = result.slice(0,n)
  console.log(result);
}
frSeq(5)

but it was still not perfect, so i simply sliced it.


#14

i hate it too. it was presented as an easy problem, well not sure about that. Thank you very very much...


#15

it is relatively easy, there are much more difficult problems to solve. See if my code can help you to make it even better, this code is okay, but not good


#16

what do u mean,because I am having trouble on the same one


#17

your code is fain, i can understand it and that is important ...thank you


#18

is your teacher okay with the fact that i help so much?


#19

as long as i am able to explain the code, he is fain, he expect us to get some help on online forums and github if we are in the pickle


#20

shouldn't the teacher be able to push you in the right direction? Do you understand the code i used?