I shouldn't be here...

JavaScript Examples

This page has some examples of JavaScript code for you to look at! You don't need to understand it at all, but we need something to help you scavenger hunt through, so this works!

Two Sum

Sum Stock Image

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

function twoSum(nums, target) {
    const map = new Map(); for (let i = 0; i < nums.length; i++) { const diff = target - nums[i]; if 
        (map.has(diff)) {
            return [map.get(diff), i];
        }
        map.set(nums[i], i);
    }
};

Reverse Integer

Reverse Stock Image

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

function reverse(x) {
  const reversed = String(Math.abs(x)).split('').reverse().join(''); if (reversed > Math.pow(2, 31)) { 
    return 0;
  }
  return reversed * Math.sign(x);
};

Palindrome Number

Palindrome Stock Image

Given an integer x, return true if x is palindrome integer. An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

function isPalindrome(x) {
  if (x < 0) return false; return Number(String(x).split('').reverse().join('')) === x;
};