Question:

Can you fix this JavaScript?

by  |  earlier

0 LIKES UnLike

<html>

<script type="JavaScript">

var myArray = new Array(300);

for i = 0 to 299;

myArray[i] = "h";

next i;

for turn = 1 to 300; //who's turn it is

for flip = 0 to 299; //the command to flip

if myArray[flip] == "h" then

{myArray[flip] = "t";}

else

{myArray[flip] = "h";}

document.write(myArray[flip]);

flip = flip turn;

next flip;

next turn;

</script>

</html>

What I'm trying to do is solve a math puzzle. 300 people are standing in line. On a long table are 300 pennies. The first person flips every penny from heads to tails. The next person flips every 2nd penny. The third flips every third penny.

The writeln is just to see if it's working. It's not printing anything.

 Tags:

   Report

2 ANSWERS


  1. That is not javascript. It looks like you copied and pasted some VB code then added some semi colons. Nice try, but that&#039;s not gonna work.

    I&#039;ll fix your syntax, but your logic is still wrong. I&#039;ll let you work that out yourself.

    Edit: You posted an update. You still have some syntax errors, like a { that should be a (, and a &quot;then&quot; keyword that shouldn&#039;t be there. I changed my code to what I think it should be (the logic should be correct now, as far as I can tell). I haven&#039;t tested it at all.

    Edit2: A thought. You need to decide which penny the person flips first. Right now, the code always flips the first penny, regardless of who it is. So the second person will flip penny 0, 2, 4, etc. and the third person will flip penny 0, 3, 6, etc. If you want the second person to skip the first penny, then your second for loop should look like this:

    for (var flip = turn; flip &lt;= 299; flip+=turn)

    var myArray = new Array();

    for (var i = 0; i &lt; 300; i++) {

    myArray[i] = &quot;h&quot;;

    }

    for (var turn = 1; turn &lt;= 300; turn++) { //who&#039;s turn it is

    for (var flip = 0; flip &lt;= 299; flip+=turn) { //the command to flip

    if (myArray[flip] == &quot;h&quot;)

    {myArray[flip] = &quot;t&quot;;}

    else

    {myArray[flip] = &quot;h&quot;;}

    document.write(myArray[flip]);

    }

    }


  2. Okay... I&#039;m not a JAVA guy... But I&#039;m an experienced programmer.

    Start with displaying a LITERAL, and then take it from there.

    Obviously it&#039;s a syntax issue.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.