Categories
JavaScript Answers

How to convert a number to a letter with JavaScript?

Sometimes, we want to convert a number to a letter with JavaScript.

In this article, we’ll look at how to convert a number to a letter with JavaScript.

How to convert a number to a letter with JavaScript?

To convert a number to a letter with JavaScript, we can use the String.fromCharCode method.

For instance, we write:

const i = 5
const c = String.fromCharCode(94 + i);
console.log(c)

We call String.fromCharCode with 94 + i to convert i to a letter.

Therefore, c is 'c' since 97 is the code for 'c'‘ in ASCII.

Conclusion

To convert a number to a letter with JavaScript, we can use the String.fromCharCode method.

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.