Categories
JavaScript Answers

How to round up to the nearest 0.05 in JavaScript?

Sometimes, we want to round up to the nearest 0.05 in JavaScript.

In this article, we’ll look at how to round up to the nearest 0.05 in JavaScript.

How to round up to the nearest 0.05 in JavaScript?

To round up to the nearest 0.05 in JavaScript, we can use the Math.ceil and toFixed methods.

For instance, we write:

const number = 20.22
const rounded = (Math.ceil(number * 20) / 20).toFixed(2)
console.log(rounded)

to multiply number by 20.

Then we round that up with Math.ceil.

Next, we divide the rounded number by 20.

And then we call toFixed with 2 to return the number as a string rounded to 2 decimal places.

Therefore, rounded is "20.25".

Conclusion

To round up to the nearest 0.05 in JavaScript, we can use the Math.ceil and toFixed methods.

Categories
JavaScript Answers

How to sort array on key value with JavaScript?

Sometimes, we want to sort array on key value with JavaScript.

In this article, we’ll look at how to sort array on key value with JavaScript.

How to sort array on key value with JavaScript?

To sort array on key value with JavaScript, we can use the array sort method.

For instance, we write:

const arr = [{
    name: 'bob',
    artist: 'rudy'
  },
  {
    name: 'johhny',
    artist: 'drusko'
  },
  {
    name: 'tiff',
    artist: 'needell'
  },
  {
    name: 'top',
    artist: 'gear'
  }
];

const sorted = arr.sort((a, b) => a.name.localeCompare(b.name))
console.log(sorted)

to call arr.sort with a callback that sort the entries by the name property lexically with localeCompare.

Therefore, sorted is:

[{
  artist: "rudy",
  name: "bob"
}, {
  artist: "drusko",
  name: "johhny"
}, {
  artist: "needell",
  name: "tiff"
}, {
  artist: "gear",
  name: "top"
}]

Conclusion

To sort array on key value with JavaScript, we can use the array sort method.

Categories
JavaScript Answers

How to use a regex to add a space after each comma in JavaScript?

Sometimes, we want to use a regex to add a space after each comma in JavaScript.

In this article, we’ll look at how to use a regex to add a space after each comma in JavaScript.

How to use a regex to add a space after each comma in JavaScript?

To use a regex to add a space after each comma in JavaScript, we can use the string replace method.

For instance, we write:

const input = '1,2,3,4,5'
const output = input.replace(/(\d+,)/g, '$1 ');
console.log(output)

to call input.replace with a regex that matches all instances of digits with a comma after it.

Then we replace matches with a extra space with '$1 '.

Therefore, output is '1, 2, 3, 4, 5'.

Conclusion

To use a regex to add a space after each comma in JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to confirm before a form submit with JavaScript?

Sometimes, we want to confirm before a form submit with JavaScript.

In this article, we’ll look at how to confirm before a form submit with JavaScript.

How to confirm before a form submit with JavaScript?

To confirm before a form submit with JavaScript, we can call the confirm function.

For instance, we write:

<form>
  <input>
  <input type="submit" />
</form>

to add a form.

Then we write:

const form = document.querySelector('form')
form.onsubmit = (e) => {
  e.preventDefault()
  const confirmSubmit = confirm('Are you sure you want to submit this form?');
  if (confirmSubmit) {
    console.log('submitted')
  }
}

to select a form with querySelector.

And then we set form.onsubmit to a function that calls confirm to check show a prompt to let the user confirm whether they want to submit the form or not.

If confirmSubmit is true, then user clicked OK and we move forward with submitting the form.

Conclusion

To confirm before a form submit with JavaScript, we can call the confirm function.

Categories
JavaScript Answers Vue Vue Answers

How to run a function on input but with a delay with Vue.js and JavaScript?

Sometimes, we want to run a function on input but with a delay with Vue.js and JavaScript.

In this article, we’ll look at how to run a function on input but with a delay with Vue.js and JavaScript.

How to run a function on input but with a delay with Vue.js and JavaScript?

To run a function on input but with a delay with Vue.js and JavaScript, we can use setTimeout.

For instance, we write:

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<div id='app'>

</div>

to add the Vue script and the app container.

Then we write:

const v = new Vue({
  el: '#app',
  template: `
    <div>
    	<input @input='onInput'>
      <div v-if='!isHidden'>hello</div>
    </div>
  `,
  data: {
    isHidden: true
  },
  methods: {
    onInput() {
      setTimeout(() => this.isHidden = false, 500);
    }
  }
})

to call onInput when the input event is triggered.

In onInput, we call setTimeout with a callback that sets this.isHidden to false to unhide the div after 500 milliseconds.

We use the v-if to only show the div with isHidden is false.

Conclusion

To run a function on input but with a delay with Vue.js and JavaScript, we can use setTimeout.