Categories
JavaScript Answers

How to find the largest palindrome made from the product of two 3-digit numbers with JavaScript?

Sometimes, we want to find the largest palindrome made from the product of two 3-digit numbers with JavaScript.

In this article, we’ll look at how to find the largest palindrome made from the product of two 3-digit numbers with JavaScript.

How to find the largest palindrome made from the product of two 3-digit numbers with JavaScript?

To find the largest palindrome made from the product of two 3-digit numbers with JavaScript, we can loop through from 999 to 100 to find the products that create palindromes with another 3 digit number.

For instance, we write:

const isPalin = (i) => {
  return i.toString() === i.toString().split("").reverse().join("");
}

const largestPalindrome = () => {
  const arr = [];
  for (let i = 999; i > 100; i--) {
    for (let j = 999; j > 100; j--) {
      const mul = j * i;
      if (isPalin(mul)) {
        arr.push(j * i);
      }
    }
  }
  return Math.max(...arr);
}

console.log(largestPalindrome());

We create the isPalin function to check if i converted to a string is the same originally as if it has been reversed with reverse.

Then we define the largestPalindrome function that has a nested loop to find the products between j and i.

And we call isPalin to check if the product is a palindrome.

And then we call Math.max with the entries of arr spread as arguments.

Therefore, we see 906609 logged.

Conclusion

To find the largest palindrome made from the product of two 3-digit numbers with JavaScript, we can loop through from 999 to 100 to find the products that create palindromes with another 3 digit number.

Categories
JavaScript Answers

How to check if a date is today with JavaScript?

Sometimes, we want to check if a date is today with JavaScript.

In this article, we’ll look at how to check if a date is today with JavaScript.

How to check if a date is today with JavaScript?

To check if a date is today with JavaScript, we can convert both dates to date strings before doing the comparison.

For instance, we write:

const today = new Date()
const today2 = new Date(2022, 0, 20)
const isSame = today.toDateString() === today2.toDateString();
console.log(isSame)

We create 2 Date instances, today and today2.

Then we call toDateString on both of them to convert them to date strings.

Then we can use === to compare the date strings.

If today is January 20, 2022, then isSame would be true.

Conclusion

To check if a date is today with JavaScript, we can convert both dates to date strings before doing the comparison.

Categories
JavaScript Answers

How to get the first property of a JSON object with JavaScript?

Sometimes, we want to get the first property of a JSON object with JavaScript.

In this article, we’ll look at how to get the first property of a JSON object with JavaScript.

How to get the first property of a JSON object with JavaScript?

To get the first property of a JSON object with JavaScript, we can use the Object.keys method.

For instance, we write:

const json = `{
  "category1": ["image/url/1.jpg", "image/url/2.jpg"],
  "category2": ["image/url/3.jpg", "image/url/4.jpg"]
}`
const obj = JSON.parse(json)
const [key] = Object.keys(obj)
console.log(key)

to define the json JSON string.

Then we parse it into an object with JSON.parse.

Then we call Object.keys with obj to return an array of the object keys.

And we get the first one with destructuring.

As a result, we see that key is 'category1'.

Conclusion

To get the first property of a JSON object with JavaScript, we can use the Object.keys method.

Categories
JavaScript Answers

How to alter and assign JavaScript object properties without side effects?

Sometimes, we want to alter and assign JavaScript object properties without side effects.

In this article, we’ll look at how to alter and assign JavaScript object properties without side effects.

How to alter and assign JavaScript object properties without side effects?

To alter and assign JavaScript object properties without side effects, we should make a copy of the original object before assigning new properties to the copied object.

For instance, we write:

const a = {
  id: 0,
  value: 1
}
const b = {
  ...a,
  id: 1
}
console.log(a === b)
console.log(b)

to make a copy of a with the spread operator and then we the id property to 1.

We can see that the console log logs false so we know a and b aren’t referencing the same object.

And we see that b is {id: 1, value: 1}.

Conclusion

To alter and assign JavaScript object properties without side effects, we should make a copy of the original object before assigning new properties to the copied object.

Categories
JavaScript Answers

How to scroll down with JavaScript?

Sometimes, we want to scroll down with JavaScript.

In this article, we’ll look at how to scroll down with JavaScript.

How to scroll down with JavaScript?

To scroll down with JavaScript, we can use the window.scrollBy method.

For instance, we write:

<div>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras vulputate neque vel dolor lobortis, a rutrum turpis sollicitudin. Praesent faucibus imperdiet euismod. Donec vitae nisl pretium, elementum nibh ac, aliquam massa. Proin eleifend mi sit amet urna consectetur, vel convallis nunc dictum. Nulla elementum, eros in congue dignissim, mi lectus aliquet augue, sed sodales odio nunc vitae nibh. Nunc id enim purus. Cras sollicitudin rhoncus enim, non tincidunt leo pellentesque nec. Etiam tristique urna eget arcu euismod, et vestibulum sem imperdiet. Morbi ultricies laoreet purus at viverra. Sed tincidunt diam quis lorem hendrerit, vitae dapibus augue congue. Aliquam erat volutpat. Curabitur lacinia libero vel metus consequat congue. Praesent nec magna quis est egestas facilisis sed id turpis. Sed finibus nulla eget purus viverra viverra. Mauris ornare quam vitae sapien tempor, eget consectetur orci maximus.
</div>

to add some content.

Then we write:

window.scrollBy(0,180);

to scroll right by 0 pixels and down by 180 pixels.

Conclusion

To scroll down with JavaScript, we can use the window.scrollBy method.