0

Try to figure out how to output concatenated text from this javascript code. The script draws a quarter circle proportional to the size of the bar and display the value of pi at three decimal places.

The script needs to display "Number of bars = " + numberOfBars. I've spent some time researching how to to this. I was wondering what is the best approach.

Here is the code so far.

<html>
    <head>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <div id="app">
            <div style="clear: left">
                <div 
                    v-for="(y, i) in bars()"
                    :key="i"
                    :style="{ width: scale, height: numberOfBars * scale, float: 'left', borderTop: (y * scale) + 'px solid blue' }"
                >
                </div>
                PI = {{ bars().reduce((a, b) => Math.PI.toFixed(3) /* TODO: replace "10" with correct expression */, 0) }}
            </div>
        </div>


        <script>

const app = new Vue({
    el: '#app',
    methods: {
        bars () {
            const bars = []
            for (let i = 0; i < this.numberOfBars; i++) {
                bars.push(Math.sqrt(this.numberOfBars * this.numberOfBars - i * i))
            }
            return bars
        },
    },
    data () {
        return {
            scale: 2,
            numberOfBars: 30,
        }
    },
})
        </script>   
    </body>
</html>

What do I need to add to get the following output? The code already displays the quarter circle and the value of pie on the web browser. The last step is to display "number of bars = 30."

enter image description here

6
  • It's unclear what you're asking here Commented Oct 31, 2020 at 9:31
  • I edited the post with more clarification. Commented Oct 31, 2020 at 9:37
  • The quarter circles aren't showing in your snippet Commented Oct 31, 2020 at 9:41
  • someone edited it earlier. Let me change it back. Commented Oct 31, 2020 at 9:43
  • 1
    Changing your snippet back to a code-block doesn't make it work, can you post working code? I'm guessing you want to add + 'px' to your width/height inline styles? Commented Oct 31, 2020 at 9:46

1 Answer 1

1

Going off your image example, you want this:

const app = new Vue({
  el: '#app',
  methods: {
    bars() {
      const bars = []
      for (let i = 0; i < this.numberOfBars; i++) {
        bars.push(Math.sqrt(this.numberOfBars * this.numberOfBars - i * i))
      }
      return bars
    },
  },
  data() {
    return {
      scale: 2,
      numberOfBars: 30,
    }
  },
})
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="app">
  <div style="clear: left">
    <div v-for="(y, i) in bars()" :key="i" :style="{ width: scale + 'px', height: numberOfBars * scale + 'px', float: 'left', borderTop: (y * scale) + 'px solid blue' }">
    </div>
    Number of bars = {{ numberOfBars }}
    <br>
    PI = {{ bars().reduce((a, b) => Math.PI.toFixed(3) /* TODO: replace "10" with correct expression */, 0) }}
  </div>
</div>

Though, all I added was Number of bars = {{ numberOfBars }}.

I also added '+ px' to your width/height styles as the snippet doesn't work otherwise.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.