Jim's
Tutorials

Fall 2018
course
site

Code attached below. I solved Euler 4 greatest palindrome product in js which was a bit of a pain in the ass. I had to implement my own reverse string function and modify the built-in sort function to sort for greatest to least. I'm encountering a testing error I havn't been able to work through yet. My example js tests look like this:

const { sum, mul, sub, div } = require("./math")

test("Adding 1 + 1 equals 2", () => {
  expect(sum(1, 1)).toBe(2)
})

and function just fineMy isPalindrome test looks like this:

const isPalindrome = require("./palindrome-product")

test("a number is a palindrome", () => {
  expect(isPalindrome(1001).toBe(true))
})

but I get this error when I try to test my isPalindrome function:

>>  FAIL  ./palindrome.test.js
  ● a number is a palindrome

    TypeError: isPalindrome is not a function

      2 |
      3 | test("a number is a palindrome", () => {
    > 4 |   expect(isPalindrome(1001).toBe(true))
        |          ^
      5 | })
      6 |

      at Object.isPalindrome (palindrome.test.js:4:10)

I've tried turning it into a const and exporting it in different ways but keep getting the same error... it seems to me like it should be a function. Not sure quite how to debug the problem.

Never mind, figured it out, mis-aligned parens... sigh :).

const {isPalindrome, reverseString} = require("./palindrome-product")

test("a number is a palindrome", () => {
  expect(isPalindrome(1001)).toBe(true)
})

test("reverse string reverses a string", () => {
  expect(reverseString("hello")).toBe("olleh")
})

This passes ^.

attachments [paper clip]

  last modified size
TXT palindrome-product.js Wed Dec 04 2024 08:32 am 822B
TXT palindrome.test.js Wed Dec 04 2024 08:32 am 555B