0

var pi = document.getElementById("pi");

function * calcpi(){
    let q = 1;
    let r = 0;
    let t = 1;
    let k = 1;
    let n = 3;
    let l = 3;
    while (true){
        if (4*q+r-t < n*t){
            alert(n);
            yield n;
            nr = 10*(r-n*t);
            n  = Math.floor((10*(3*q+r))/t)-10*n;
            q  *= 10;
            r  = nr;
        } else {
            nr = (2*q+r)*l;
            nn = Math.floor((q*(7*k)+2+(r*l))/(t*l));
            q  *= k;
            t  *= l;
            l  += 2;
            k += 1;
            n  = nn;
            r  = nr;
        }
    }
}

var pi_digits = calcpi();
var i = 0;
window.alert(pi_digits.next());
for (let j = 0; j < 10; j++){
    pi.textContent += toString(pi_digits.next());
    if (i == 100){
        i = 0; pi.textContent+= "\n"
    } else {
        i += 1;
    }
}
Pi: <br><span id="pi"></span>

I'm trying to get the first 100 digits of pi... I'm basing the code off of this https://replit.com/@Cloverwave/CalcPi?v=1 from https://rosettacode.org/wiki/Pi#Python Whenever I run this code my window gets alerted the right numbers, but it doesn't show those numbers on the page... I've been going off this: What's the yield keyword in JavaScript?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • No I'm not looking for https://rosettacode.org/wiki/Pi#JavaScript but a version based off of the code I made – robin_piefi Sep 11 '22 at 14:28
  • 1
    It does show something for me… It's not a number, but why this happens should become clear if you replace `toString(pi_digits.next())` with `JSON.stringify(pi_digits.next())` – Bergi Sep 11 '22 at 14:35
  • I figured as much... how do I take the value though? – robin_piefi Sep 11 '22 at 14:38
  • Did you try the `JSON.stringify` to see what objects it does return? Just access the relevant property on them. – Bergi Sep 11 '22 at 14:42
  • var pi_digits = calcpi(); console.log(pi_digits.next().value); pi.textContent = toString(pi_digits.next().value); When only doing this at the end of the function, the console log gets a different answer than the webpage – robin_piefi Sep 11 '22 at 14:45
  • Yes, because you're calling `.next()` twice now. – Bergi Sep 11 '22 at 14:45
  • Also don't use the global `toString(…)` - that is [`window.toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString) and doesn't take an argument. Call `String(pi_digits.next().value)` or `pi_digits.next().value.toString()`. – Bergi Sep 11 '22 at 14:48

1 Answers1

0

nvm I found the answer all I had to do was use .value behind .next() and remove the toString() function. Thanks to Bergi and this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield

<!DOCTYPE html>
<html>
    <body>
        
        Pi: <br><span id="pi"></span>
        
        <script>
//            window.alert("ok");
        
            var pi = document.getElementById("pi");
            
            function * calcpi(){
                let q = 1;
                let r = 0;
                let t = 1;
                let k = 1;
                let n = 3;
                let l = 3;
                while (true){
                    if (4*q+r-t < n*t){
                        yield n;
                        nr = 10*(r-n*t);
                        n  = Math.floor((10*(3*q+r))/t)-10*n;
                        q  *= 10;
                        r  = nr;
                    } else {

                        nr = (2*q+r)*l;
                        nn = Math.floor((q*(7*k)+2+(r*l))/(t*l));
                        q  *= k;
                        t  *= l;
                        l  += 2;
                        k += 1;
                        n  = nn;
                        r  = nr;
                    }
                }
            }
            
            var pi_digits = calcpi();
            var i = 0;
            for (let j = 0; j < 10; j++){  
//                window.alert("jo");
                 
                pi.textContent += pi_digits.next().value;
                if (i == 100){
                    i = 0; pi.textContent += "\n"
                } else {
                    i += 1;
                }
            }
//            
            
        </script>
        
    </body>
</html>