Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get a return value?

Is there a way to see why this question is being closed? Ahh, if you click on the circled arrow – show timeline, we can see Needs details or clarity was the reason this post was closed. Clearly, there’s an accepted answer, so just because these three couldn’t understand the question someone else could and did. Just another example of the thought police on stackoverflow. The op is clearly a newbee and looking for some help. Why not think about trying to be helpful instead of just closing the question and not commenting on why?

In JavaScript is there any way of stopping a function from carrying out an action but still get its return value? I would like to obtain the currentPosition of a game piece on a game board. What would be the best way to do this?

Here is some of my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let pieceOne = document.getElementById('piece-one');
let pieceTwo = document.getElementById('piece-two');
let squares = document.getElementsByClassName('board-square');
let currentPosition1 = 0;
let currentPosition2 = 0;
const board = [
    'recharge',
    'city-garden',
    'earthship',
    'hydroponics',
    'ev-car',
    'greenway',
    'cycle-path',
    'greenspace',
    'hydroelectric',
    'solar-farm',
    'community-work',
    'habitat-survey'
];

function movePiece() {
    let diceRoll = rollDice();
    if (currentPlayer === 'player1') {
        currentPosition1 += diceRoll;
        if (currentPosition1 >= board.length) {
            currentPosition1 -= board.length;
        }
        let currentSquare = squares[currentPosition1];
        currentSquare.appendChild(pieceOne);
        currentPlayer = players[1];
    } else {
        currentPosition2 += diceRoll;
        if (currentPosition2 >= board.length) {
            currentPosition2 -= board.length;
        }
        let currentSquare = squares[currentPosition2];
        currentSquare.appendChild(pieceTwo);
        currentPlayer = players[0];
    } if (currentPlayer = players[1]) {
        return currentPosition1;
    } else {
        return currentPosition2;
    }
}

I don’t know if it’s possible but I would like to return the values without the dice rolling or the pieces moving.

>Solution :

define position as a var giving it global scope. separate the movePosition() from
getPosition(). the trick here is making position global. let will define the variable within its scope. Of course, you don’t need a function to get position

var position;
movePiece();
console.log(getPosition())
console.log(position);

function movePiece() {
  position = 5;

}

function getPosition() {
  return position;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading