Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is my code:
JavaScript
  1  // 1. deposit money
  2  // 2. determine number of lines to bet on
  3  // 3. collect a bet amount
  4  // 4. spin the slot machine
  5  // 5. check if the user won or lost
  6  // 6. give the winnings or take the money
  7  // 7. play again
  8  
  9  
 10  
 11  const prompt =  require("prompt-sync")();
 12  
 13  // 4. spin the slot machine
 14  
 15  const ROWS = 3;
 16  const COLS = 3;
 17  
 18  const SYMBOL_COUNT = { //THIS IS KNOWN AS AN OBJECT
 19      A:2,
 20      B:4,
 21      C:6,
 22      D:8
 23  };
 24  
 25  const SYMBOL_VALUES = {
 26      A:5,
 27      B:4,
 28      C:3,
 29      D:2
 30  };
 31  
 32  
 33  // 1. deposit money
 34  
 35  function deposit() {
 36      while(true) { // this is to make sure that the user enters a valid amount
 37      const depositAmount = prompt("Enter your deposit amount: ");
 38      const numberDepositAmount = parseFloat(depositAmount); // this will turn a string into an integer
 39       if (isNaN(numberDepositAmount)/* this chcks if something is not a number*/ || numberDepositAmount <= 0){
 40          console.log('Invalid deposit amount, please try again');
 41      } else{
 42          return numberDepositAmount; // this will break the while loop
 43          }
 44      }
 45  };
 46  
 47  
 48  // 2. determine number of lines to bet on
 49  
 50  function getNumberOfLines(){
 51      while(true) { // this is to make sure that the user enters a valid amount
 52          const lines = prompt("Enter the number of lines (1-3): ");
 53          const numberOfLines = parseFloat(lines); // this will turn a string into an integer
 54           if (isNaN(numberOfLines)/* this chcks if something is not a number*/ || numberOfLines <= 0 || numberOfLines > 3){
 55              console.log('Invalid amount, please try again');
 56          } else{
 57              return numberOfLines; // this will break the while loop
 58              }
 59          }
 60  }
 61  
 62  // 3. collect a bet amount
 63  
 64  function getBet(balance,lines)/* it is going to be based on the balance*/ {
 65      while(true) { // this is to make sure that the user enters a valid amount
 66          const bet = prompt("Enter the bet per line: ");
 67          const totalBet = parseFloat(bet); // this will turn a string into an integer
 68           if (isNaN(totalBet)/* this chcks if something is not a number*/ || totalBet <= 0 || totalBet > balance / numberOfLines)/* this will check if there is enough*/{
 69              console.log('Invalid amount, please try again');
 70          } else{
 71              return totalBet; // this will break the while loop
 72              }
 73          }
 74  }
 75  
 76  // 4. spin the slot machine
 77  
 78  function spin (){
 79      const symbols = [];// this is known as an array
 80  
 81      for (const[symbol, count] of Object.entries(SYMBOL_COUNT)){
 82          for (let i = 0; 1 < count; i++){
 83              symbols.push(symbol);
 84          }
 85      }
 86      const reels = [[],[],[]]; // this represents the coloumns
 87      for (let i = 0; i < COLS; i++){
 88          const reelSymbols = [...symbols];
 89          for(let j = 0; j < ROWS; j++){
 90              const randomIndex = Math.floor(Math.random() * reelSymbols.length);
 91              const selectedSymbol = reelSymbols[randomIndex];
 92              reels[i].push(selectedSymbol); //index i is gonna represent thge reel we are working on
 93              reelSymbols.splice(randomIndex, 1); // the one here is to remove one element 
 94              
 95          }
 96      }
 97      return reels;
 98  };
 99  
100  const reels = spin();
101  console.log(spinReel); // to check if code is working
102  let balance =  deposit(); // its not a constant bcz later the balance will adjusted based on what they are betting
103  const numberOfLines = getNumberOfLines();
104  const betamount = getBet(balance, numberOfLines);

And this is the error that i get:
# Fatal error in , line 0
# Fatal JavaScript invalid size error 169220804
#
# FailureMessage Object: 000000315D5FEA60       
 1: 00007FF6E100FEEF node_api_throw_syntax_error+206383 2: 00007FF6E0F21A8F v8::CTypeInfoBuilder<void>::Build+11951
 3: 00007FF6E1D8E8B2 V8_Fatal+162
 4: 00007FF6E18D0615 v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArray+101
 5: 00007FF6E1755C93 v8::internal::FeedbackNexus::ic_state+65795
 6: 00007FF6E1774810 v8::Context::GetIsolate+15600     
 7: 00007FF6E15C0F70 v8::internal::CompilationCache::IsEnabledScriptAndEval+25952
 8: 00007FF6E1ACDA41 v8::internal::SetupIsolateDelegate::SetupHeap+558193
 9: 00007FF661C0D00B


What I have tried:

I'm not sure what the problem is even when I tried to watch the video I couldn't find the issue, I used a debugger, a code checker etc, but there was no problem there and now I'm really confused.
Posted
Updated 12-Jul-23 2:23am
v2

1 solution

The error points to line 1 (0) where you reference the 'prompt-sync' package. Make sure you have installed this package by running 'npm install prompt-sync' in your project directory.

I have picked up on some other items that will error also -

1) Your inner loop condition in the spin function is incorrect. Instead of '1 < count', it should be 'i < count' so that the loop runs for the correct number of times -
JavaScript
for (let i = 0; i < count; i++){


2) Your 'console.log' statement is using an incorrect variable name. It should be 'reels' instead of 'spinReel' -
JavaScript
console.log(reels);


3) You have a typo in the variable name 'betamount'. It should be 'betAmount' (capital 'A') -
JavaScript
const betAmount = getBet(balance, numberOfLines);
 
Share this answer
 
Comments
Hamza Ayub 12-Jul-23 8:31am    
i fixed all of these it still shows the same error, and I reinstalled prompt-sync
Andre Oosthuizen 12-Jul-23 9:16am    
Your Javascript run-time might also be the cause for it to crash as it starts. Make sure you have a proper JavaScript runtime environment set up. Make sure that you have Node.js installed on your machine and that you are executing the code using the Node.js runtime.
Hamza Ayub 13-Jul-23 9:21am    
thank you sir it was the the problem you told above, where the 1 was supposed to be i thank you
Andre Oosthuizen 13-Jul-23 12:14pm    
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