0

I have a sequence that needs to be an output.. which is 8 so what i did (with a help of a friend of course.. bec i'm not smart and have adhd.. is this:

let a = 1;
let b = 1;
let c = a + b;
a = b; // 1
b = c; // 2
c = a + b; // 3
a = b; // a = 2
b = c; // b = 3
c = a + b; // 2 + 3 = 5    c = 5

what i need next, is to make a for loop, that will calculate it to 8 (perform the exact same calc task as above (a = b and b = c and c = a + b)

to make the function get print an output of 8..

let a = 1;
let b = 1;
let c = a + b;
a = b; // 1
b = c; // 2
c = a + b; // 3
a = b; // a = 2
b = c; // b = 3
c = a + b; // 2 + 3 = 5    c = 5
function calc () {
    for (let i=0; i<= ( ? ? ? ? ? ?); i++){
            console.log(i);
        }
    }
calc()
  • 2
    This is the Fibonacci Sequence, right? Have you tried googling it? – Barmar Nov 01 '22 at 19:23
  • @Barmar yes I have tried everything I have in my power right now.. this is so frustrating. the answer will help me figure out the next tasks. I do not care about this specific task. I just need to know how loops work, and to figure it out, I need to know how to complete this piece of work. was I doing it correctly? the loop itself is missing the parenthesis but I am talking about the whole code. also, what needs to be in the parenthesis? – JavaScriptStudent Nov 01 '22 at 19:25

3 Answers3

1

function calc () {
  for (let a = 0, b = 1, c = 0; c < 8;) {
    c = a + b;
    a = b;
    b = c;
    console.log(c);
  }
}
calc();

does it work as you need? The main idea here: in the loop you can use any variables to set initial state, increment and break condition.

UPDATE

The loop simply repeats what you've done without loop. Let's see where every part of your code goes.

Loop declaration contains 3 parts.

  1. Initialization
let a = 1;
let b = 1;

I just updated a to be 0, not 1. So number 1 will be displayed in your console.

  1. Exit condition.

As you described, loop should end when we reach 8. We increment the values inside the loop and then display it. So condition should be c < 8.

  1. "Increment"

In your case it's

let c = a + b;
a = b; // 1
b = c; // 2

This block is repeated several times and contains more than 1 assignment. So we put it inside the block and leave the "increment" part empty.

Alexey Zelenin
  • 710
  • 4
  • 10
  • Zelnin Yes but why did u make it so complicated? all i asked is what is the number i am supposed to put in my current for loop function. it is impossible for me to understand it now even more – JavaScriptStudent Nov 01 '22 at 19:51
0

It sounds like you're trying to output the Fibonacci sequence (1, 1, 2, 3, 5, 8, 13...) where each number in the sequence is the sum of the two previous numbers.


function calc() {
    // We need to hard-code the first two numbers in the sequence to get started
    let a = 1;
    let b = 1;
    console.log(a);
    console.log(b);

    // If we do 4 loops, the last value printed will be 8
    for (let i = 0; i < 4; i++) {
        let c = a + b
        console.log(c);
        // Shift a and b along the sequence
        // a gets the value held by b, and b gets the value held by c
        a = b
        b = c
    }
}
calc();

If you want it to only print out 8.

function calc() {
    let a = 1;
    let b = 1;

    for (let i = 0; i < 4; i++) {
        let c = a + b
        a = b
        b = c
    }
    console.log(b);  // Prints 8
}
calc();

Tom
  • 51
  • 1
  • 5
  • ok, but if i want to follow this exact assignment (without the DOM part) Instead of hardcoding Y (the result of the Fibonacci of X), calculate it with a for loop The calculation should be wrapped in a function, that gets X as an argument, and returns Y After the function, you should call the function, and assign the returned value in your HTML to present to the user what should i do? i am talking about the calling of the function part. thanks. – JavaScriptStudent Nov 01 '22 at 20:44
0

Yes, a Fibonacci series - what fun! Here is a slightly condensed version of it with only two variabes (a and b):

function calc (max) {
  for (let a = 0, b = 1; b < max;) console.log(([a,b] = [b,a+b])[1])
}
calc(100);

Update

Here a version that will only return the last two values (for later use in your HTML): the last one below and the first one above the target value max:

function calc (max) {
  for (var a = 0, b = 1; b < max;) [a,b] = [b,a+b];
  return [a,b];
}
console.log(calc(100));

Sorry for overloading you. I have to admit, I didn't carefully read all the text in your question. In particular your ADHD situation. No worries, I will try to explain what I did now in more detail so you will be able to follow:

1. My loop is set up in a for() loop - like in Alexey's solution:
for (var a = 0, b = 1; b < max;)

This means

  • that initially the variables a and b will be set upt to contain the values 0 and 1 (the two assignemts are separated by the ,-opertor).
  • The second part of the for expression (b < max) checks whether bis still below the maximum value max and only then will the loop expression be executed.
  • A third expression (behind the ;-separator) does not exist.
2. The loop expression ...
[a,b] = [b,a+b]

... makes use of the new ES6 feature of destructuring. It is a shorthand version of the otherwise three separate assignement statements:

 tmp = b;  // buffer the value of b 
 b = a+b;  // do the sum and re-assign to b
 a = tmp;  // assign the original value of b to variable a

The ES6 construct is helpful here,

  • as it buffers the calculated results of the right hand side before assigning it to the array on the left hand side of the equation.
  • Only then (in the destructuring phase) will the newly calculated values be assigned to the variables a and b.
3. The return value(s)

As I was uncertain as to which value you wanted to have returned from the function I chose to return two values in an array: one below and one above the target value max.

return [a,b];

It is up to you to adapt the return statement to better suit your needs: either return a; or return b;.

4. var instead of let

Please note that I used var a=0, b=1 instead of let a=0, b=1 in the for() loop of my later version, as I needed to access these variables outside the scope of the for() loop. The var defines a variable with function scope (instead of block scope).

I hope that this will help you a little more than my admittedly fairly cryptic first version.

Alternative version (variation)

If you are looking for a function that will retun the n-th value of the Fibonacci series you can do it like this:

function fib (n) {
  for (var a = 0, b = 1; n--;) [a,b] = [b,a+b];
  return b;
}

// test the function fib with n = 1, ..., 19
for (let i=1; i<20; i++) console.log(`fib(${i}):`,fib(i));
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • you made it shorter, but harder to understand.. what are all the brackets there and the parenthesis for? also, what is that 1 at the right with brackets between? i cannot understand it.. please read: Instead of hardcoding Y (the result of the Fibonacci of X), calculate it with a for loop The calculation should be wrapped in a function, that gets X as an argument, and returns Y After the function, you should call the function, and assign the returned value in your HTML to present to the user which means all of it needs to be differently within the func.. how can i do that? – JavaScriptStudent Nov 01 '22 at 20:29