0

I'm trying to work on a project and I only understand bits and pieces of what I need to be doing. Goal: create a web page that prompts the user to enter a number which represents how many temperatures the user intends to enter. Then, the user will enter that amount of temperatures one at a time, and when the amount has been reached, the program display each of the temperatures entered on its own line on the screen, and then show the average of the temperatures that have been collected.

My current problem is that I don't know how to create an array from prompt input. The prompts are the way I would like them to be, but after that I'm lost. I wrote an idea of what I think I'm looking for, but that may not even be the right direction. After I figure the array part out I'll need to figure out how to average the numbers, but that's not my biggest concern. The document.write() at the bottom is the layout for what I would like to ultimately appear on the screen.

var number = parseInt( window.prompt("How many temperatures would you like to average?"));
for (var i = 0; i < number; i++) {
    window.prompt("Enter temperature")
}
var temps = new Array(number[i]);
for (var j = 0; j < temps.length; j++); {
    document.write(temps[j] + "<br>");
}
document.write("There are" + number + "temperatures to average." + "<br>" + temps[j] + "<br>"
        + "The average of your temperatures is:" + avg);
5
  • 2
    Create an array and push the responses from the "Enter temperature" prompt into it, eg const temps = []; for (let i = 0; i < number; i++) temps.push(prompt("Enter temperature")) Commented Oct 8, 2020 at 2:05
  • 2
    You need to assign the result of window.prompt() to a varable. Commented Oct 8, 2020 at 2:05
  • 1
    number[i] is wrong. number is not an array, it's the length of the array you want to create. Commented Oct 8, 2020 at 2:07
  • 2
    use a form, prompt will drive you nuts Commented Oct 8, 2020 at 2:07
  • 4
    If your teacher is telling you to use document.write() to display your web page, tell them to join the 21st century. Commented Oct 8, 2020 at 2:08

1 Answer 1

-1

With only a few extra lines of code, you can accomplish this without using window.prompt() and is a little be more of a friendlier user experience.

Maybe it's of some interest.

new Vue({
  el: '#app',
  computed: {
    avarage: function() {
      if (!this.result.length) return 0;
      return this.result.reduce((a, b) => a + b, 0) / this.result.length
    }
  },
  data() {
    return {
      temperatures: 0,
      result: []
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.14/vue.min.js"></script>

<div id="app">
  <div>
    How many temperatures would you like to average?<br>
    <input type="text" v-model.number="temperatures" />
  </div>

  <div v-if="temperatures">
    <h4>Temps</h4>
    <div v-for="(temp, index) in temperatures">
      {{index+1}}. <input type="text" @input="$set(result, index, parseInt($event.target.value, 10))" placeholder="enter temperature" />
    </div>

    <h4>Avarage Temp</h4>
    <div>{{ avarage }}</div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.