0

ok, got to make a program that allows a user to enter data into the text fields (specifically a name and a grade), it then needs to store it locally, it then needs to be able to create a graph, and work out the standard deviation and mean from the grade. i was informed that using a cookie might be the best option. but that it would involve splitting the cookie everytime the user add a new record.

the things i'm having trouble with are...

1) Accepting input from a text field and storing it in a cookie
2) Spitting a cookie in the correct place.

am i on the right lines? or do i need to do it a different way?

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Taywin
0

Is the page static? What I mean is that do you need to reload the whole page again after a set of data is entered? If not, you can create a global variable or a javascript object to store the data each time a user adds the value. Then draw it into a specific area. Below is a sample code for the whole page. You need some modification & addon to serve your requirement.

<html>
<head>
<script type="text/javascript">
var storage = new Array();  // global variable

function add2Storage(id) {
  var el = document.getElementById(id)
  if (el && el.value.length>0) {  // prevent non-existing element
    if (el.value.match(/^\d+$/)) {  // integer only
      storage.push(parseInt(el.value,10))
      alert("Added "+el.value)
    }
    else {
      alert("'"+el.value+"' is not a valid integer number")
    }
    el.value = ""
    el.focus()
  }
}

function drawAGraph(id) {
  var el = document.getElementById(id)
  if (el) {
    var out = "<br>Graph<br>"
    for (var i=0; i<storage.length; i++) {
      for (var j=0; j<storage[i]; j++) {
        out += "|"
      }
      out += "("+storage[i]+")<br>"
    }
    out += "<br>"
    out += "Mean: "+getMean()+"<br>"
    out += "SD: "+getSD()+"<br>"
    el.innerHTML = out
  }
}

function getSD() {
  var mean = getMean()
  var sd=0, sdSum = 0, tmp
  for (var i=0; i<storage.length; i++) {
    tmp = storage[i]-mean
    sdSum += (tmp*tmp)
  }
  if (storage.length>0) {
    sd = Math.sqrt(sdSum/getElemNum())
  }
  return sd
}

function getMean() {
  return (getSum()/getElemNum())
}

function getSum() {
  var sum = 0
  for (var i=0; i<storage.length; i++) {
    sum += storage[i]
  }
  return sum
}

function getElemNum() {
  return storage.length
}
</script>
</head>

<body>
  <input type="text" id="enter_val">&nbsp;&nbsp;
  <input type="button" value="Add Value" onclick="add2Storage('enter_val')">
  <input type="button" value="Draw Graph" onclick="drawAGraph('draw_area')">
  <br><br>
  <div id="draw_area">
  </div>
</body>
</html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.