Solution: Number guessing - history



examples/arrays/guess_number_history.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">

  <title>Guess Number</title>
</head>
<body>

<input id="n">
<button id="go">Guess</button>

<div id="result"></div>

<script src="guess_number_history.js"></script>

</body>
</html>

examples/arrays/guess_number_history.js
"use strict";

var hidden = Math.round(Math.random()*200+0.5);
var guess_history = [];
var i;

console.log(hidden);

function check_guess() {
    var n = Number(document.getElementById('n').value);
    var result = 'equal after ' + guess_history.length + ' guesses.';
    if (n < hidden) {
        result = n + ' is less than our number';
    }
    if (n > hidden) {
        result = n + ' is greater than our number';
    }
    if (guess_history.length > 0 && n !== hidden) {
        //console.log(guess_history[ ]);
        if (Math.abs(guess_history[guess_history.length - 1] - hidden) < Math.abs(n - hidden)) {
            result += ' (<b>cold</b>)';
        } else {
            result += ' (<b>warm</b>)';
        }
    }
    guess_history.push(n);
    result += '<br>';
    result += '<b>History:</b><br>'
    for (i = guess_history.length-1; i >=0; i--) {
        result += guess_history[i] + '<br>';
    }
    document.getElementById('result').innerHTML = result;
    return false;
}

document.getElementById('go').addEventListener('click', check_guess);