Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Help Please

  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help Please

    I need help on these 3 coding subjects for my class. Which all involves looping.

    My first question is:
    Write a program that uses a prompt box to ask the user to input a word. Using a for statement, print each letter of the word (on a separate line) in order using document.write
    This is the code I have for it, to me it seems alright but its not doing anything.

    <script type="text/Javascript">
    var wordIn = prompt("Please enter a word","");
    var len = wordIn.length;

    for (var count = 0; count <= len-1; ++count)
    {
    var letter = wordIn.charAt(count);
    document.write(letter):
    document.write("<br />");
    }
    document.write("Word entered was : " + wordIn);
    document.write("<hr />")
    </script>


    My second question is:
    Write a program that requests a number (from 1 to 20) and displays that many asterisks (*) using document.write. Display these all on the same row. Make sure that ONLY numbers 1-20 are allowed in the prompt box. If the user enters a number out of range, use an alert box to inform the user and also display that information using document.write

    This is the code I have for that and I'm kinda lost on this one.
    <script type ="text/Javascript">
    //Question 7
    var number = prompt("Please enter the number of asterisks from 1-20","");

    for (var count = 1; count <= number; ++count)

    if (number <=1)
    {
    alert("You didn't entered a right number." + number)
    }
    else
    if (number <=20)
    {
    document.write (number + "<br>")
    }
    else
    {
    alert("You didn't entered a right number." + number)
    }

    </script>


    Now my final question, I am totally lost on how I'm able to get this to work.

    Write a program that uses a prompt box to request a positive integer as input and carry out the following algorithm. If the number is even, divide it by 2. If the number is odd, multiply the number by 3 and then add 1. Repeat this process with the resulting number and continue repeating the process until number 1 is reached. The output should display the results of each iteration, and also how many iterations were required to reach number 1.

    Many thanks to the person or people who help me out here.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    My first question is:
    Write a program that uses a prompt box to ask the user to input a word. Using a for statement, print each letter of the word (on a separate line) in order using document.write
    This is the code I have for it, to me it seems alright but its not doing anything.
    Here's a good opportunity for you to practice some Basic Debugging 101.

    (Think syntax)

    My second question is:
    Write a program that requests a number (from 1 to 20) and displays that many asterisks (*) using document.write. Display these all on the same row. Make sure that ONLY numbers 1-20 are allowed in the prompt box. If the user enters a number out of range, use an alert box to inform the user and also display that information using document.write
    As you loop through the FOR loop, build a string of *'s by appending a new * on each iteration. Then just after the loop, output the string.

    Write a program that uses a prompt box to request a positive integer as input and carry out the following algorithm. If the number is even, divide it by 2. If the number is odd, multiply the number by 3 and then add 1. Repeat this process with the resulting number and continue repeating the process until number 1 is reached. The output should display the results of each iteration, and also how many iterations were required to reach number 1.
    To determine if a number is odd or even you can use the % operator.
    Last edited by bullant; 05-03-2011 at 04:20 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •