Click here to Skip to main content
15,935,728 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code:

// 1. deposit money
// 2. determine number of lines to bet on
// 3. collect a bet amount
// 4. spin the slot machine
// 5. check if the user won or lost
// 6. give the winnings or take the money
// 7. play again



const prompt =  require("prompt-sync")();

// 4. spin the slot machine

const ROWS = 3;
const COLS = 3;

const SYMBOL_COUNT = { //THIS IS KNOWN AS AN OBJECT
    A:2,
    B:4,
    C:6,
    D:8
};

const SYMBOL_VALUES = {
    A:5,
    B:4,
    C:3,
    D:2
};


// 1. deposit money

function deposit () { // this is anothert way of making functions using es6
    while(true) { // this is to make sure that the user enters a valid amount
    const depositAmount = prompt("Enter your deposit amount: ");
    const numberDepositAmount = parseFloat(depositAmount); // this will turn a string into an integer
     if (isNaN(numberDepositAmount)/* this chcks if something is not a number*/ || numberDepositAmount <= 0){
        console.log('Invalid deposit amount, please try again');
    } else{
        return numberDepositAmount; // this will break the while loop
        }
    }
};


// 2. determine number of lines to bet on

function getNumberOfLines () {
    while(true) { // this is to make sure that the user enters a valid amount
        const lines = prompt("Enter the number of lines (1-3): ");
        const numberOfLines = parseFloat(lines); // this will turn a string into an integer
         if (isNaN(numberOfLines)/* this chcks if something is not a number*/ || numberOfLines <= 0 || numberOfLines > 3){
            console.log('Invalid amount, please try again');
        } else{
            return numberOfLines; // this will break the while loop
            }
        }
}

// 3. collect a bet amount

function getBet (balance,lines)/* it is going to be based on the balance*/ {
    while(true) { // this is to make sure that the user enters a valid amount
        const bet = prompt("Enter the bet per line: ");
        const totalBet = parseFloat(bet); // this will turn a string into an integer
         if (isNaN(totalBet)/* this chcks if something is not a number*/ || totalBet <= 0 || totalBet > balance / numberOfLines)/* this will check if there is enough*/{
            console.log('Invalid amount, please try again');
        } else{
            return totalBet; // this will break the while loop
            }
        }
}

// 4. spin the slot machine

function spin() {
    const symbols = [];// this is known as an array

    for (const[symbol, count] of Object.entries(SYMBOL_COUNT)){
        for (let i = 0; i < count; i++){
            symbols.push(symbol);
        }
    }
    const reels = []; // this represents the coloumns
    for (let i = 0; i < COLS; i++){
        reels.push([]); // this is made so that there is no erro is the no. cols is more than 3
        const reelSymbols = [...symbols];
        for(let j = 0; j < ROWS; j++){
            const randomIndex = Math.floor(Math.random() * reelSymbols.length);
            const selectedSymbol = reelSymbols[randomIndex];
            reels[i].push(selectedSymbol); //index i is gonna represent thge reel we are working on
            reelSymbols.splice(randomIndex, 1); // the one here is to remove one element 
            
        }
    }
    return reels;
};

function convertCols(reels){
    const rows = [];
    for(let i = 0; i < ROWS; i++){ // now for every single row, it will loof through every single coloumn
        rows.push([]);
        for(let j = 0; j < COLS; j++){
            rows[i].push(reels[j][i]); // here ew are accessing each coulomn then we are getting the elements in the rows of the coloumns
        }
    }
    return rows;
}

function printRows (rows){
    for (row of rows){
        let rowString = "";
        for(const [i, symbol] of row.entries()){
            rowString += row;
            if( i != row.length - 1){
                rowString += " | ";
            }
        }
        console.log(rowString);
    }
}


let balance =  deposit(); // its not a constant bcz later the balance will adjusted based on what they are betting
const numberOfLines = getNumberOfLines();
const betAmount = getBet(balance, numberOfLines);
const reels = spin();
const convert_cols = convertCols(reels);
const print_rows =  printRows(rows);

// 5. check if the user won or lost


the error I get is this:

ReferenceError: rows is not defined
    at Object.<anonymous> (D:\slot-machine\script.js:131:31) 
    at Module._compile (node:internal/modules/cjs/loader:1119:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
    at Module.load (node:internal/modules/cjs/loader:997:32) 
    at Module._load (node:internal/modules/cjs/loader:838:12)    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:18:47

Node.js v18.9.0


What I have tried:

i am pretty sure i have defined rows on the function above it and It still gives me the error.
Posted
Updated 14-Jul-23 0:12am
v2

1 solution

Defining a variable as a function parameter limits the scope of the variable to that function only (i.e. only between the "{" and "}" that delimit the function body) it only exists inside 6the code block for that function and can't be accessed outside it.

Think about it: a single function can be called several times with different values in the parameter. If you could somehow access the parameter value outside the function, which of those values would you expect to get? All of them? The first? The last? What about calls which are in the code but haven't been executed yet? Should they be considered?
You can't get any - they do not exist once the function has stopped executing!
 
Share this answer
 
Comments
Hamza Ayub 14-Jul-23 6:15am    
ok thank you
OriginalGriff 14-Jul-23 10:44am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900