Snakes and Ladders Javascript Coding Help!

Started on the old forum in February 2011 · view the original in the archive · old links redirect here

2 replies · 2,870 views · started 15 years ago · 2 min read

3 participants
gsoh81 Newbie · 1 posts #1

😕 Hello Every body, Hi I'm stuck on a javascript problem. I have a set of instructions with declared variables and functions that is meant to simulate the game of snakes and ladders. I have used the instructions to attempt it but keep getting undefined results where the squres with the snakes and ladders are unrecognised? I was wandering if Anyone could point me in the right direction. Sorry it's such a long post. I have included my attempt at a solution below aswell.

INSTRUCTIONS call the function rollDie and store the return value as the player’s score write out the score add the score to the player’s position write out a message saying what square the player is on call the function findIndexOf with suitable arguments to look for the index of the player’s position in the special squares array and store the value returned if the index is not -1 set the player’s position to the number of the square at the same index in the connected squares array write out a message saying what square the ladder has taken the player to end if write out a line break

<HTML> <HEAD> <TITLE> Snakes and Ladders </TITLE> <SCRIPT LANGUAGE = "JavaScript"> /* *imitates a normal 6-sided die, by returning a random whole number between 1 and 6 inclusive. */ function rollDie() { return Math.floor(Math.random() * 6) + 1; } /* *searches for a number in a number array. * *function takes two arguments * the number to search for * the number array to search *function returns the array index at which the number was found, or -1 if not found. */ function findIndexOf(number, numberArray) { var indexWhereFound = -1; for (var position = 0; position < numberArray.length; position = position + 1) { if (numberArray[position] == number) { indexWhereFound = position; } } return indexWhereFound; } //ARRAYS that represent the board -- you do not need to change these //array of special squares var specialSquaresArray = [1,7,25,32,39,46,65,68,71,77]; //array of corresponding squares the player ascends or descends to var connectedSquaresArray = [20,9,29,13,51,41,79,73,35,58]; //VARIABLES used -- you do not need to change these //the square the player is currently on var playersPosition; //play is initially at START playersPosition = 0; //what they score when they roll the die var playersScore; //the index of the player's position in the special squares array or -1 var indexOfNumber; MAIN PROGRAM - MY ATTEMPT! playersScore = rollDie(); document.write('Your Score is: ' + playersScore); playersPosition = playersScore + playersPosition; document.write(' You are on Square ' + playersPosition); indexOfNumber = findIndexOf(playersPosition, specialSquaresArray); document.write(' ladder to square ' + connectedSquaresArray[indexOfNumber]); document.write(' '); </SCRIPT> </HEAD> <BODY> </BODY> </HTML>

Any suggestions greatly appreciated! Thank you.

gotaheadache Newbie · 1 posts #2

Snakes and Ladders Javascript Coding Help!

Hi gsoh81

It appears we're going the same course 🙂

I've managed to get a value returned for the ladder to square statement. Refer to your M150 (thats if you're doing this course of course) course book 8 page 24 section 2.7. That should help you.

bridie78 Newbie · 1 posts #3

Hi,

I am doing this course too and also struggling with it. At the moment, my program runs but I get astronimical numbers and my output looks something like this :

Score: 40 Square: 44 Score: 54 Square: 95 Score: 69 Square: 156 Score: 215 Square: 172 Score: 117 Square: 181 Score: 518 Square: 235 Score: 423 Square: 274 Score: 427 Square: 314 Score: 131 Square: 321 Score: 632 Square: 386 Score: 638 Square: 446 Score: 344 Square: 473 Score: 147 Square: 481 Score: 648 Square: 546 Score: 254 Square: 562 Score: 456 Square: 604 Score: 560 Square: 655 Score: 165 Square: 661 Score: 166 Square: 671 Score: 467 Square: 714 Score: 271 Square: 732 Score: 473 Square: 774 Score: 677 Square: 836 Ladder to square:83

Does anyone know where I'm going wrong here. I thought the random number (rollDie) bit was already set to return a random number between 1-6, so why am I getting such high numbers??

Any help is much appreciated.

Thanks

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close