Categories
Vue 3 Projects

Create a Grade Calculator with Vue 3 and JavaScript

Vue 3 is the latest version of the easy to use Vue JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a grade calculator with Vue 3 and JavaScript.

Create the Project

We can create the Vue project with Vue CLI.

To install it, we run:

npm install -g @vue/cli

with NPM or:

yarn global add @vue/cli

with Yarn.

Then we run:

vue create grade-calculator

and select all the default options to create the project.

We also need to install the uuid package so that we can generate unique IDs for each grade entry.

To install it, we run:

npm i uuid

Create the Grade Calculator

To create the grade calculator, we write:

<template>
  <form @submit.prevent="calculate">
    <div v-for="(grade, index) of grades" :key="grade.id">
      <label>grade</label>
      <input v-model.number="grade.value" />
      <button type="button" @click="deleteGrade(index)">delete grade</button>
    </div>
    <button type="button" @click="addGrade">add grade</button>
    <button type="submit">calculate average</button>
  </form>
  <div>Your average grade is {{ average }}</div>
</template>

<script>
import { v4 as uuidv4 } from "uuid";
export default {
  name: "App",
  data() {
    return {
      grades: [{ id: uuidv4(), value: 0 }],
      average: 0,
    };
  },
  computed: {
    formValid() {
      return this.grades.every(({ value }) => !isNaN(+value));
    },
  },
  methods: {
    addGrade() {
      this.grades.push({ id: uuidv4(), value: 0 });
    },
    deleteGrade(index) {
      this.grades.splice(index, 1);
    },
    calculate() {
      if (!this.formValid) {
        return;
      }
      this.average =
        this.grades.map(({ value }) => value).reduce((a, b) => a + b, 0) /
        this.grades.length;
    },
  },
};
</script>

We have a form with the @submit directive to let us handle form submissions with the calculate method.

The prevent modifier lets us do client-side submissions instead of server-side.

Inside it, we render divs with v-for for each grade entry.

Inside each div, we have an input field that binds to the grade.value property with v-model .

The number modifier converts the input value to a nmber.

The delete grade button is set to type button so that it doesn’t submit the form when we click it.

It calls deleteGrade to delete the grade entry at the given index .

Below that, we have the add grade button to add a grade by calling addGrade when we click it.

And below that, we have the button to submit the form.

The div is used to display the average grade.

In the script tag, we have the data method that returns the grades array and average .

They’re both reactive properties.

The formValid computed property checks if every grades entry’s value property is a number with the isNaN function.

Below that, we have the methods we call.

The addGrade method calls push to add a new entry into the this.grades array.

uuidv4 returns a UUID string so we can add an ID to the entry.

We need the unique ID so that Vue 3 can discern the entries that are rendered.

We set that as the key prop’s value to make it a unique identifier for the row.

The deleteGrade method calls splice to remove an item with the index and 1 to remove the entry with the given index .

The calculate method calls map to return an array with the value s of each grades entry.

Then it calls reduce to add the numbers together. The 0 in the 2nd argument is the initial value.

And then the total is divided by this.grades.length to get the average.

Conclusion

We can create a grade calculator easily with Vue 3 and JavaScript.

Categories
Vue 3 Projects

Create a Temperature Converter with Vue 3 and JavaScript

Vue 3 is the latest version of the easy to use Vue JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a temperature converter with Vue 3 and JavaScript.

Create the Project

We can create the Vue project with Vue CLI.

To install it, we run:

npm install -g @vue/cli

with NPM or:

yarn global add @vue/cli

with Yarn.

Then we run:

vue create temperature-converter

and select all the default options to create the project.

Create the Temperature Converter

To create the temperature converter, we write:

<template>
  <form @submit.prevent="convert">
    <div>
      <label>temperature in celsius</label>
      <input v-model.number="celsius" />
    </div>
    <button type="submit">convert</button>
  </form>
  <div>{{ celsius }}c is {{ fahrenheit }}f</div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      celsius: 0,
      fahrenheit: 0,
    };
  },
  computed: {
    formValid() {
      return !isNaN(+this.celsius);
    },
  },
  methods: {
    convert() {
      if (!this.formValid) {
        return;
      }
      this.fahrenheit = this.celsius * (9 / 5) + 32;
    },
  },
};
</script>

We create the form with an input that takes the temperature in celsius.

We bind the input value to the celsius reactive with v-model .

The number modifier lets us convert it to a number automatically.

In the form , we add the @submit directive to listen for clicks on the button with type submit .

The prevent modifier prevents server-side submissions and lets us do client-side submissions.

Then in the component object, we have the data method that returns the celsius and fahrenheit reactive properties.

We have the formValid computed property that checks if the celsius reactive property is a number.

Then in the convert method,. we check the formValid computed property to see if the form is valid.

Then if it is, we compute the fahrenheit value from the celsius value.

And finally, in the template, we display the results of the convert in the div.

Conclusion

We can create a temperature easily with Vue 3 and JavaScript.

Categories
Vue 3 Projects

Create a Palindrome Checker with Vue 3 and JavaScript

Vue 3 is the latest version of the easy to use Vue JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a palindrome checker with Vue 3 and JavaScript.

Create the Project

We can create the Vue project with Vue CLI.

To install it, we run:

npm install -g @vue/cli

with NPM or:

yarn global add @vue/cli

with Yarn.

Then we run:

vue create palindrome-checker

and select all the default options to create the project.

Create the Palindrome Checker

To create the palindrome checker, we write:

<template>
  <form>
    <div>
      <label>word to check</label>
      <input v-model="word" />
    </div>
  </form>
  <div>Is Palindrome:{{ isPalindrome ? "Yes" : "No" }}</div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      word: "",
    };
  },
  computed: {
    isPalindrome() {
      const { word } = this;
      return word === word.split("").reverse().join("");
    },
  },
};
</script>

We have the input field in the form.

And we bind the inputted value to the word reactive property with v-model .

Then below that, we have the data method that returns the word reactive property.

Then we add the isPalindrome computed property to check if this.word is a palindrome.

We check if it’s a palindrome by checking if the reversed string is the same as the original.

We call split to split it into an array of characters.

Then we call reverse to reverse the array.

And then we call join to join the characters back into a string.

Then we display that value in the template.

Conclusion

We can create a palindrome checker easily with Vue 3 and JavaScript.

Categories
JavaScript

Add Charts to Our JavaScript App with Anychart — Step Area Charts, Step Line Charts, Stick Charts, and Sunburst Charts

Anychart is an easy to use library that lets us add chart into our JavaScript web app.

In this article, we’ll look at how to create basic charts with Anychart.

Step Area Chart

We can create a step area chart with Anychart.

To add one, we write the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

Then we write the following JavaScript code:

const data = [{
    x: "1995",
    value: 0.10
  },
  {
    x: "1996",
    value: 0.10
  },
  {
    x: "1997",
    value: 0.12
  },
  {
    x: "1998",
    value: 0.13
  },
  {
    x: "1999",
    value: 0.15
  },
];

const chart = anychart.area();
const series = chart.stepArea(data);
series.stepDirection("forward");
chart.container("container");
chart.draw();

We add the Anychart base package with the script tag.

Then we add a div which is used as the container for the chart.

Next, we add the data array with the x and value properties in each entry.

x has the x-axis value.

value has y-axis value.

anychart.area creates the area chart.

chart.stepArea creates the step area chart with our data.

series.stepDirection sets the direction of the steps.

chart.container sets the ID for the container element for the chart.

chart.draw draws the chart.

Step Line Chart

We can create step line charts in a similar way.

For instance, we can write:

const data = [{
    x: "1995",
    value: 0.10
  },
  {
    x: "1996",
    value: 0.10
  },
  {
    x: "1997",
    value: 0.12
  },
  {
    x: "1998",
    value: 0.13
  },
  {
    x: "1999",
    value: 0.15
  },
];

const chart = anychart.stepLine();
const series = chart.stepLine(data);
series.stepDirection("forward");
chart.container("container");
chart.draw();

The data is the same as the previous example.

anychart.stepLine creates the step line chart.

chart.stepLine sets the data.

The rest of the code is the same as the step area chart example.

Stick Chart

We can create a stick easily with Anychart.

To add it, we write:

const data = [{
    x: "1995",
    value: 0.10
  },
  {
    x: "1996",
    value: 0.10
  },
  {
    x: "1997",
    value: 0.12
  },
  {
    x: "1998",
    value: 0.13
  },
  {
    x: "1999",
    value: 0.15
  },
];

const chart = anychart.stick();
const series = chart.stick(data);
chart.container("container");
chart.draw();

We call anychart.stick to create the stick chart.

chart.stick creates the stick chart with our data.

The rest of the code is the same as the other examples.

Sunburst Chart

We can create a sunburst chart with Anychart.

To do this, we write:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-sunburst.min.js"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

to add the Anychart base and sunburst chart packages and the container div.

Then we write:

const data = [{
  name: "Company A",
  children: [{
      name: "Technical",
      children: [{
          name: "Architects"
        },
        {
          name: "Developers"
        },
      ]
    },
    {
      name: "Sales",
      children: [{
          name: "Analysts"
        },
        {
          name: "Executives"
        }
      ]
    },
    {
      name: "HR"
    },
  ]
}];

const chart = anychart.sunburst(data, "as-tree");
chart.container("container");
chart.draw();

to add the data and the chart.

data has the name and children properties to add the name of the segment and the children segment contents respectively.

Then we call anychart.sunburst to create the sunburst chart with the data .

'as-tree' lets us display the data in the chart as a tree structure.

The rest of the code is the same as the previous examples.

Conclusion

We can add step area charts, step line charts, stick charts, and sunburst charts easily with Anycharts.

Categories
JavaScript

Add Charts to Our JavaScript App with Anychart — Sparklines, Spline Charts, and Spline Area Charts

Anychart is an easy to use library that lets us add chart into our JavaScript web app.

In this article, we’ll look at how to create basic charts with Anychart.

Sparkline

We can create a sparkling easily with Anycharts.

To add one, we write the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-sparkline.min.js"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

Then we add the following JavaScript code:

const stage = anychart.graphics.create('container');
const chart = anychart.sparkline();
chart.container(stage);
chart.data([1.1371, 1.1341, 1.1132, 1.1381, 1.1371]);
chart.bounds(0, 0, 90, 20);
chart.draw();

We add the script tags for the Anychart base package the sparkline package.

Then we add the div which will be used as the container element for rendering the sparkline in.

Next, we create the stage object with the anychart.graphics.create method.

We pass in the ID for the container element to render the sparkline in.

Then we call anychart.sparkline to create the sparkline.

And we add that to the stage by calling chart.container .

chart.data sets the data for the chart.

chart.bounds sets the top left and bottom right corners of the sparkline respectively.

The numbers are in pixels.

chart.draw draws the sparkline.

Spline Area Chart

We can add a spline area chart with Anychart.

For instance, we can write the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

And the following JavaScript code:

const data = [{
    x: "January",
    value: 43
  },
  {
    x: "February",
    value: 46
  },
  {
    x: "March",
    value: 12
  },
  {
    x: "April",
    value: 65
  },
  {
    x: "May",
    value: 71
  }
];

const chart = anychart.area();
const series = chart.splineArea(data);
chart.container("container");
chart.draw();

We only need the Anychart base package to create the spline area chart.

The container div is the same as the previous example.

Next, we create the data array with the data for our chart.

x has the x-axis values.

value has the y-axis values.

anychart.area creates the area chart.

chart.splineArea adds the spline area chart with the data.

The rest of the code is the same as the previous example.

Spline Chart

We can create a spline chart with the anychart.line method.

For instance, we can write:

const data = [{
    x: "January",
    value: 43
  },
  {
    x: "February",
    value: 46
  },
  {
    x: "March",
    value: 12
  },
  {
    x: "April",
    value: 65
  },
  {
    x: "May",
    value: 71
  }
];

const chart = anychart.line();
const series = chart.spline(data);
chart.container("container");
chart.draw();

We call anychart.line to create the line chart.

Then we create the spline chart with chart.spline .

The rest of the code is the same as the other examples.

Conclusion

We can create sparklines, spline charts, and spline area charts with Anychart.