hi all have a homework project have to come up with a html code for ducci's 4 number game using a fuction called difference(firstNumber secondNumber) can anyone help if i get this wrong i am in serious trouble please help

Dani AI

Generated

The thread is about implementing one Ducci step (and typically iterating it) for four numbers. posted a broken script and was right to ask for code; posted a working single-step example. Below are the concrete problems to watch for, a minimal correct approach, and safe testing tips so the logic is clear and repeatable.

Common problems in the posted attempt: syntax errors (extra commas in parameter lists, stray braces), misspelling length, re-declaring parameters as different values, wrong loop increment expressions, using two separate arrays where a single array and wrap-around indexing are needed, and failing to return values from functions. Also avoid using document.write for debugging in modern code; use console logging or breakpoints. Use modulo (i+1) % n for the last-element wrap-around and Math.abs when the absolute difference is required.

Minimal, clear implementation (original and compact):

function absDiff(a, b) {
  return Math.abs(a - b);
}

function nextDucci(seq) {
  var n = seq.length;
  var out = new Array(n);
  for (var i = 0; i < n; i++) {
    var j = (i + 1) % n;
    out[i] = absDiff(seq[i], seq[j]);
  }
  return out;
}

To run the full Ducci process and stop on all zeros or a repeat (cycle detection):

function iterateDucci(start, maxIter) {
  var seen = {};
  var cur = start.slice();
  for (var k = 0; k < maxIter; k++) {
    if (cur.every(function(x){ return x === 0; })) return {status: 'zeros', steps: k};
    var key = cur.join(',');
    if (seen[key]) return {status: 'cycle', steps: k};
    seen[key] = true;
    cur = nextDucci(cur);
  }
  return {status: 'max', steps: maxIter};
}

Testing tips: run small inputs in the browser console, step through with breakpoints, test the wrap-around case explicitly, and set a sensible maxIter to avoid infinite loops. For background theory, see the Ducci sequence entry: Ducci sequence.

Recommended Answers

All 10 Replies

Its not learning if your making someone else do it

i dont want the answer i just need a little shoov in the right direction

Its not learning if your making someone else do it

i only need a shoov in the right direction can you help

you start, we steer - once you begin
There are announcements above all the forums about homework
just how PO everybody who will answer is about homework
show a code example, and many will help straight off, otherwise you just get answers like this

hi all this is what i have so far and its not working please help

<script>
var numberArray = [16,14,4,5];
var diffArray = new Array(4);
function difference(firstNumber, secondNumber,)
{
var firstNumber = numberArray.lenght;
var secondNumber = numberArray.lenght;
{
for (var count = 0; count < numberArray.length; count = count + 1)
{
diffArray[count] = firstNumber[count] - secondNumber[count + 1];
}

for (var count = 0; count < diffArray.length; count = numberArray + 1)
{
if (firstNumber > secondNumber)
{
firstNumber - secondNumber;
}
else
{
secondNumber - firstNumber;
}
}
{
document.write(difference() + ',');
}
}}
</SCRIPT>

Your code is full of syntax errors. I think this is what you want. But, you really need to learn this stuff on your own.

<script>
difference();
function difference() {
	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>

You can't use this for your answer on TMA03 question 4 of the m150open uni course as it is using java that is not in the books yet!

im not at uni im only 15 still at school my teachures say i dont have the brains to go to uni

im not at uni im only 15 still at school my teachures say i dont have the brains to go to uni

Your using the same values as the question and the same function names. I'm not stupid so don't take me for a fool! Oh and in the other forum you are even showing the java with the same comments on it and date!

you can use html input object,


hi all have a homework project have to come up with a html code for ducci's 4 number game using a fuction called difference(firstNumber secondNumber) can anyone help if i get this wrong i am in serious trouble please help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.