Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need a better outcome in javascript please 1

Status
Not open for further replies.

newtojavascript

Technical User
Feb 6, 2010
4
GB
Hi

Can anyone help me with this javascript please
as you can see i have given it a far shot but just not getting the end result.
What i am trying to do is to list the outcome each time i click enter in a window like this

Code:
16,14,4,5
2,10,1,11
8,9,10,9
1,1,1,1
0,0,0,0
Iterations taken: 4


any help would be great as i am getting tired now after a week of trying

thanks
sue
aka (Newtojavascript)
xxx


Code:
<HTML>
<HEAD>
<TITLE>
Ducci's Game 
</TITLE>

<SCRIPT LANGUAGE = "JavaScript">

//Write your functions here.
difference();
function difference() {
	

//Main program.
//Array which holds the numbers at the four corners of the square.
var numberArray = [16,14,4,5];//Edit this line to change the starting numbers
document.write(numberArray +'<BR>');//Write out array. 


//Write the rest of the main program here.
var numberArray = [16,14,4,5];
var diffArray = new Array(4);
var count = 0;
var numLen = numberArray.length;
for (count = 0; count < numLen; count++) 
{
if ((count + 1) == numLen) 
{
diffArray[count] = numberArray[count] - numberArray[0];
alert("a " +numberArray[count] + " - " + numberArray[0] + " = " + diffArray[count]);
} 
else 
{
diffArray[count] = numberArray[count] - numberArray[count + 1];
alert("b " + numberArray[count] + " - " + numberArray[count + 1] + " = " + diffArray[count]);
}
}
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

 
Hi

JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]difference[/color][teal]([/teal]numberArray[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] numLen[teal]=[/teal]numberArray[teal].[/teal]length
  [b]var[/b] iteration[teal]=[/teal][purple]0[/purple]
  [b]var[/b] diffArray
  [b]var[/b] zero

  document[teal].[/teal][COLOR=darkgoldenrod]writeln[/color][teal]([/teal]numberArray[teal]+[/teal][green][i]'<br>'[/i][/green][teal])[/teal]

  [b]do[/b] [teal]{[/teal]
    diffArray[teal]=[][/teal]
    [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal];[/teal]i[teal]<[/teal]numLen[teal];[/teal]i[teal]++)[/teal] diffArray[teal].[/teal][COLOR=darkgoldenrod]push[/color][teal]([/teal]Math[teal].[/teal][COLOR=darkgoldenrod]abs[/color][teal]([/teal]numberArray[teal][[/teal]i[teal]]-[/teal]numberArray[teal][([/teal]i[teal]+[/teal][purple]1[/purple][teal])%[/teal]numLen[teal]]))[/teal]
    numberArray[teal]=[/teal]diffArray
    zero[teal]=[/teal][b]true[/b]
    [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal];[/teal]i[teal]<[/teal]numLen[teal];[/teal]i[teal]++)[/teal] [b]if[/b] [teal]([/teal]numberArray[teal][[/teal]i[teal]])[/teal] zero[teal]=[/teal][b]false[/b]
    document[teal].[/teal][COLOR=darkgoldenrod]writeln[/color][teal]([/teal]numberArray[teal]+[/teal][green][i]'<br>'[/i][/green][teal])[/teal]
    iteration[teal]++[/teal]
  [teal]}[/teal] [b]while[/b] [teal](![/teal]zero[teal])[/teal]

  document[teal].[/teal][COLOR=darkgoldenrod]writeln[/color][teal]([/teal][green][i]'Iterations taken : '[/i][/green][teal]+[/teal]iteration[teal]+[/teal][green][i]'<br>'[/i][/green][teal])[/teal]
[teal]}[/teal]

[COLOR=darkgoldenrod]difference[/color][teal]([[/teal][purple]16[/purple][teal],[/teal][purple]14[/purple][teal],[/teal][purple]4[/purple][teal],[/teal][purple]5[/purple][teal]])[/teal]

Feherke.
 
hi thanks for that feherke ;)

ok only problem i am having now is that it says

Code:
syntax error
Line: 21
Source Code:
var numberArray = [16,14,4,5]; //Edit this line to change the starting numbers
Apart from this one issue the program runs sweet as a nut.
thanks for your help feherke

regards
Sue
 
Hi

For the solution I posted no other JavaScript code is needed. The starting numbers are passed as parameters to the difference() function. So the line with error you quoted is not necessary.

Feherke.
 
DUH !!!!!

silly me
now i am embarrassed hahaha

thanks again for your help on this matter

problem well and truly sorted thanks

sue
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top