samedi 25 février 2017

Javascript: Random number generator won't produce random number

// Need a global variable:
var tasks = []; 

// Function called when the form is submitted.
// Function adds a task to the global array.
function addTask() {
    'use strict';

    // Get the task:
    var task = document.getElementById('task');

    // Reference to where the output goes:
    var output = document.getElementById('output');
    
    // For the output:
    var message = '';

    if (task.value) {
    
        // Add the item to the array:
        tasks[tasks.length] = task;
                
                //Generate a random task
                var randomTask = tasks[Math.floor(Math.random()*tasks.length)].value;
                
        // Update the page:
        message = 'You have ' + tasks.length + ' task(s) in your to-do list.' + ' ' + randomTask;
                if (output.textContent !== undefined) {
                        output.textContent = message;
                } else {
                        output.innerText = message;
                }
        
    } // End of task.value IF.

    // Return false to prevent submission:
    return false;
    
} // End of addTask() function.

// Initial setup:
function init() {
    'use strict';
    document.getElementById('theForm').onsubmit = addTask;
} // End of init() function.
window.onload = init;
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>To-Do List</title>
    <!--[if lt IE 9]>
    <script src="http://ift.tt/oHTnA7"></script>
    <![endif]-->
    <link rel="stylesheet" href="css/form.css">
</head>
<body>
    <!-- tasks.html -->
        <form action="#" method="post" id="theForm">
            <fieldset><legend>Enter an Item To Be Done</legend>
                <div><label for="task">Task</label><input type="text" name="task" id="task" required></div>
                <input type="submit" value="Add It!" id="submit">
          <div id="output"></div>
            </fieldset>
        </form>
    <script src="js/tasks.js"></script>
</body>
</html>

I am trying to make a task list that stores all of the tasks inputted into an array where afterwards I'll display a random task on the webpage. But when I test it, it will only produce the last thing I inputted on the screen. Where am I making my error?

// Need a global variable:
var tasks = []; 

// Function called when the form is submitted.
// Function adds a task to the global array.
function addTask() {
    'use strict';

    // Get the task:
    var task = document.getElementById('task');

    // Reference to where the output goes:
    var output = document.getElementById('output');

    // For the output:
    var message = '';

    if (task.value) {

        // Add the item to the array:
        tasks[tasks.length] = task;

        //Generate a random task
        var randomTask = tasks[Math.floor(Math.random()*tasks.length)].value;

        // Update the page:
        message = 'You have ' + tasks.length + ' task(s) in your to-do list.' + ' ' + randomTask;
        if (output.textContent !== undefined) {
            output.textContent = message;
        } else {
            output.innerText = message;
        }

    } // End of task.value IF.

    // Return false to prevent submission:
    return false;

} // End of addTask() function.

// Initial setup:
function init() {
    'use strict';
    document.getElementById('theForm').onsubmit = addTask;
} // End of init() function.
window.onload = init;




Aucun commentaire:

Enregistrer un commentaire