/    Sign up×
Community /Pin to ProfileBookmark

Beginner needs a little help please

Hello, im really new to javascript and having been stuck on a issue for the past hours…i just can’t seem to find a solution, ive tried everything i can think of! i’m stuck with the bolded area, what im intending is to when “position” reaches one of “specialPositionArray” values, “position” then takes the value parallel to specialPositionArray in the index of “connectedPositionArray”.
Any help would be greatly appreciated, i just dont seem to be ableeo think outside the box well enough yet!

function random()
{
return Math.floor(Math.random() * 6) + 1;
}

function findIndexOf(number, numberArray)
{
var indexOfNumber = -1;
for (var position = 0; position < numberArray.length; position = position + 1)
{
if (numberArray[position] == number)
{
indexOfNumber = position;
}
}
return indexOfNumber;
}

var specialPositionArray = [1,7,25,32,39,46,65,68,71,77];
var connectedPositionArray = [20,9,29,13,51,41,79,73,35,58];
random(‘Score’);
document.write(‘Score: ‘ + random(Score) + ‘ ‘);
position = position + random();
document.write(‘Square: ‘ + position + ‘ ‘);
findIndexOf(position, specialPositionArray);
[B][I]if (indexOfNumber == specialPositionArray)
{
document.write(‘move to position: ‘ + position);

}
document.write(‘<BR>’);[/I]
[/B]

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@cluefulJan 18.2011 — Your main problem is that you're trying to access private variables outside their scope.

Also learn to use the error console. In Firefox it's under 'Tools'.
Copy linkTweet thisAlerts:
@Trevor78authorJan 18.2011 — Sorry i' kind of messed the copy and paste, and i can't find an edit button :o

I've been trying everything i can from my limited knowledge. My main problem at the moment is with the following small bit of code. I'm trying to call the function findIndexOf using the value from playersPosition and the specialSquaresArray, once the value is stored it will check if the value from playersPosition is stored on any of the elements of the specialSquaresArray and if so it will execute the write ladder. I just can't find any way to do ... noone told me coding was so time comsuming! :p

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;

}

[B]findIndexOf(playersPosition, specialSquaresArray);



if (playersPosition == specialSquaresArray(findIndexOf))

{

document.write(' Ladder:' + playersPosition);[/B]
Copy linkTweet thisAlerts:
@thraddashJan 18.2011 — Loads of undefined variables...

[CODE]function random()
{
return Math.floor(Math.random() * 6) + 1;
}

function findIndexOf(number, numberArray)
{
var indexOfNumber = -1;
for (var position = 0; position < numberArray.length; position = position + 1) {
if (numberArray[position] == number) {
indexOfNumber = position;
}
}
return indexOfNumber;
}

var specialPositionArray = [1,7,25,32,39,46,65,68,71,77];
var connectedPositionArray = [20,9,29,13,51,41,79,73,35,58];
random('Score');
document.write('Score: ' + random([COLOR="Red"]Score[/COLOR]) + ' ');
[COLOR="red"]position [/COLOR]= position + random();
document.write('Square: ' + position + ' ');
findIndexOf(position, specialPositionArray);
if ([COLOR="red"]indexOfNumber [/COLOR]== specialPositionArray) {
document.write('move to position: ' + position);
}
document.write('<br />'); [/CODE]
Copy linkTweet thisAlerts:
@thraddashJan 18.2011 — What are you expecting this code to do?

Like clueful stated, you are trying to use variables that are defined within functions, those values are not accessible from the outside, such as [B]position [/B]and [B]indexOfNumber[/B].

You pass out [B]indexOfNumber [/B]from [B]findIndexOf[/B], but you don't store it anywhere.

You are also passing parameters to the [B]random [/B]function that doesn't expect any.

The [B]connectedPositionArray[/B] array is never used?

Coding is a great way to waste time ? The more time you give to it, the better you get.
Copy linkTweet thisAlerts:
@Trevor78authorJan 18.2011 — Trying a new script
Copy linkTweet thisAlerts:
@thraddashJan 18.2011 — I added a variable, I got no errors since...

[CODE]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 = 0;
//play is initially at START

//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

//TODO add code here
rollDie(playersScore);
document.write('Score: ' + rollDie(playersScore));
playersPosition = rollDie() + playersPosition;
document.write(' Square: ' + playersPosition);
[COLOR="Red"]var indexWhereFound [/COLOR]= findIndexOf(playersPosition, specialSquaresArray);

if (playersPosition == specialSquaresArray[indexWhereFound]) {
document.write(' Ladder:' + playersPosition);
}
document.write('<br />');[/CODE]
Copy linkTweet thisAlerts:
@Trevor78authorJan 18.2011 — You my friend, have just releived my stress! Tank you VERY VERY much! ?
Copy linkTweet thisAlerts:
@thraddashJan 18.2011 — LOL, ok?

I still don't understand what [B]connectedSquaresArray [/B]is for though.

Looks like your using a program to move players on a board game?
Copy linkTweet thisAlerts:
@Trevor78authorJan 18.2011 — Yes its a simulated board game ? i'm adding the code as instructed, so not sure when i'll have to use connectedSquaresArray yet, Thanks again! ?
×

Success!

Help @Trevor78 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...