Var and let simple beginner problem


Joined
Nov 8, 2019
Messages
1
Reaction score
0
Hello, this is an elementary js problem,which is currently simplified until I get the below problem solved.
In function <b>select</b> I select 2 people at random.
In function <b>transfer</b>, there is a transfer of money from poorer to richer .
poorer and richer are calculated in function <b>select</b>
Problem is that richer and poorer are not recognised when control goes to function <b>transfer</b>.
I get "undefined" on line 45.
I know this is a simple matter of var and let, but I have not yet found a simple explanation of var and let and hoist on the web. Simple for a beginner like me, that is.
If I can get this sorted the rest will be easy.
Can anyone help?
Is there a reasonable site which explains var and let for a beginner?
And the significance of "use strict"?
I am retired and self-taught.
I was unable to copy/paste the .html from VSCode so here it is.


JavaScript:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ginaCoeff</title>
</head>

<body>
    <script>
        var richer;
        var poorer;
        var N = 10;
        var startLevel = 100;
        var people = [];
        var trials = 100;
        var transferFrac = 0.20;

        function loadPeople(N) {
            for (let i = 0; i <= N - 1; i++) {
                people[i] = startLevel;
                console.log(i, people[i]);
            }
        }

        function select(N, richer, poorer) {
            var x = Math.floor(Math.random() * N) + 1;
            var y = Math.floor(Math.random() * N) + 1;
            if (x == y) {
                x = x + Math.floor(Math.random() * N) + 1;
                if (x > N) {
                    x = x - N;
                }
            }
            richer = Math.max(x, y);
            poorer = Math.min(x, y);
            console.log("fn select: " + "richer " + richer + "  poorer " + poorer);
        }

        function transfer(richer, poorer, transferFrac) {
            var richer;
            var poorer;
            console.log("fn transfer1: " + "richer " + richer + "  poorer " + poorer);
            var transferAmount = people[poorer] * transferFrac;
            people[richer] = people[richer] + transferAmount;
            people[poorer] = people[poorer] - transferAmount;
        }

        /////////////////////////////////////////////////////

        loadPeople(N);
        select(N, richer, poorer);
        transfer(richer, poorer, transferFrac);
        console.log(people[richer] + "   " + people[poorer]);


    </script>
</body>

</html>
 
Ad

Advertisements

Joined
Nov 27, 2019
Messages
163
Reaction score
24
For some reason the select function does not change the globals 'richer' and 'poorer' at all. Maybe the difference between a string and a numeric. Don't know, don't care. I changed the call so I can get a return from the function.
I used Number() in the func. to insure I had a number and returned both. Then I change it to a string and found the values. This should be evident for the most part/. The if loop may need to be explained.

The returned value is converted to a string I can obtain the char. at any spot in the string. The highest value for poorer is 9 ( a single digit) so I find the length and take one less because the string numbering starts at 0. Next I could take the char at 0, but it could be 10 and that is two chars - the reason for the loop.
In the
transfer func, you use a different type of array then what you first made and loaded so I just did things to the two variables I had. Check out your array, it's numeric people[1] and people[25], etc.. And each item in the array is set to 100.
Here's the final code:
Code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ginaCoeff</title>
</head>
<body>
<script>
var richer;
var poorer;
var N = 10;
var startLevel = 100;
var people = [];
var trials = 100;
var transferFrac = 0.20;
function loadPeople(N) {
    for (let i = 0; i <= N - 1; i++) {
        people[i] = startLevel;
        //alert(i+', '+ people[i]);
    }
}
function select(N, richer, poorer) {
    var x = Math.floor(Math.random() * N) + 1;
    var y = Math.floor(Math.random() * N) + 1;
    if (x == y) {
        x = x + Math.floor(Math.random() * N) + 1;
        if (x > N) {
            x = x - N;
        }
    }
    richer = Number(Math.max(x, y));
    poorer = Number(Math.min(x, y));
    //alert("fn select: " + "richer " + richer + "  poorer " + poorer);
    return richer+'-'+poorer;
}

function transfer(richer, poorer, transferFrac) {alert(richer);alert(poorer);
    var richer;
    var poorer;
    var transferAmount = poorer * transferFrac;
    richer = richer + transferAmount;
    poorer = poorer - transferAmount;
    alert("fn transfer1: " + "richer " + richer + "  poorer " + poorer);
}

loadPeople(N);
var tiny = select(N, richer, poorer);
tiny.toString();
poorer = tiny.charAt(tiny.length - 1);
if(tiny.length == 4) {
    richer = 10 
}else {
    richer = tiny.charAt(0)
}
transfer(richer, poorer, transferFrac);
//alert(people[richer] + "   " + people[poorer]);
</script>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top