19
loading...
This website collects cookies to deliver better user experience
// provided in requirements
if user_input not number return false
// negative number cant be palindrome
if user_input is less then 0 return false
// any positive number in range of 1 to 10 is valid palindrome
if user_input is in range of 1..10 return user_input
// if number is bigger then 10,
// then we shall gradually divide our initial user_input into
// left-comparison half & right-comparison half
// once we divided into two halfs
// we shall compare the halfs and return the comparison result
while left-comparison-half > right-comparison-half
// collect the most right number from user_input
// to the right-comparison half
right-comparison-half: collect user_input's most right number
// remove the most right number from the left-comparison half
left-comparison-half: = remove user_input's most right number
// compare the collected halfs and return the result
return left-comparison-half === right-comparison-half
I am using Jest library npm i -D jest
You might find useful to adjust your test command to watch over the files so the tests will be re-executed on every file change
"scripts": { "clear_jest": "jest --clearCache", "test": "jest --watchAll --verbose" },
describe("Numeric Palindrome", () => {
it.todo("should be initialized with any input type")
it.todo("should be able to manage it's state")
it.todo("validation method should be defined")
it.todo("return false if data is not numeric")
it.todo("return false if negative number")
it.todo("return false if data is 10 dividable")
it.todo("return true if data is smaller then 10")
it.todo("return true if legal palindrome")
it.todo("return false if not legal palindrome")
})
it.todo("should be initialized with any input type")
it("should be initialised with any input type",
() => {
const palindromInstances = [
new Palindrome("abc"),
new Palindrome(),
new Palindrome(1),
new Palindrome({})
]
palindromInstances.forEach(instance => expect(instance).toBeDefined())
}
);
class Palindrome {
constructor() { }
}
module.exports = Palindrome
const Palindrome = require('./numeric-palindrome')
describe("Numeric Palindrome", () => {
it.todo("should be able to manage it's state")
it("should be able to manage it's state", () => {
const palindromeOne = new Palindrome('1');
const palindromeTwo = new Palindrome();
const palindromeThree = new Palindrome(1);
expect(palindromeOne).toHaveProperty("data", "1");
expect(palindromeTwo).toHaveProperty("data", "");
expect(palindromeThree).toHaveProperty("data", 1);
})
class Palindrome {
constructor(userInput = '') {
this._data = userInput
}
get data() {
return this._data
}
}
it.todo("validation method should be defined")
it("validation method should be defined", () => {
const palindrome = new Palindrome()
expect(palindrome.isValid()).toBeDefined()
})
class Palindrome {
constructor(userInput = '') {
this._data = userInput
}
get data() {
return this._data
}
isValid() {
return false
}
}
it.todo("return false if data is not numeric")
it("return false if data is not numeric", () => {
const notNumeric = [new Palindrome("a"), new Palindrome(), new Palindrome({})]
notNumeric.forEach(x => expect(x.isValid()).toBeFalsy())
})
class Palindrome {
constructor(userInput = '') {
this._data = userInput
}
get data() {
return this._data
}
isValid() {
if (!Number.isInteger(this._data)) {
return false
}
return true
}
}
it.todo("return false if negative number")
it("return false if negative number", () => {
const negativeNumber = new Palindrome(-1)
expect(negativeNumber.isValid()).toBeFalsy()
})
isValid() {
if (!Number.isInteger(this._data)) {
return false
}
if (this._data < 0) {
return false
}
return true
}
// requiriments
const Palindrome = require('./numeric-palindrome')
describe("Numeric Palindrome", () => {
it("should be initialised with any input type",
() => {
const palindromInstances = [
new Palindrome("abc"),
new Palindrome(),
new Palindrome(1),
new Palindrome({})
]
palindromInstances.forEach(instance => expect(instance).toBeDefined())
}
);
it("should be able to manage it's state", () => {
const palindromeOne = new Palindrome('1');
const palindromeTwo = new Palindrome();
const palindromeThree = new Palindrome(1);
expect(palindromeOne).toHaveProperty("data", "1");
expect(palindromeTwo).toHaveProperty("data", "");
expect(palindromeThree).toHaveProperty("data", 1);
})
it("validation method should be defined", () => {
const palindrome = new Palindrome()
expect(palindrome.isValid()).toBeDefined()
})
it("return false if data is not numeric", () => {
const notNumeric = [new Palindrome("a"), new Palindrome(), new Palindrome({})]
notNumeric.forEach(x => expect(x.isValid()).toBeFalsy())
})
it("return false if negative number", () => {
const negativeNumber = new Palindrome(-1);
expect(negativeNumber.isValid()).toBeFalsy();
})
it("return false if data is 10 devidable", () => {
const tenDivision = [new Palindrome(10), new Palindrome(20), new Palindrome(150)];
tenDivision.forEach(sample => expect(sample.isValid()).toBeFalsy())
})
it("return true if data is smaller then 10", () => {
const underTen = [new Palindrome(1), new Palindrome(2), new Palindrome(9)];
underTen.forEach(sample => expect(sample.isValid()).toBeTruthy())
})
it("return false if not legal palindrome", () => {
const invalidPalindromes = [new Palindrome(1112), new Palindrome(112), new Palindrome(12)]
invalidPalindromes.forEach(sample => expect(sample.isValid()).toBeFalsy())
})
it("return true if legal palindrome", () => {
const validPalindromes = [new Palindrome(111), new Palindrome(11), new Palindrome(1)]
validPalindromes.forEach(sample => expect(sample.isValid()).toBeTruthy())
})
})
// implementation
class Palindrome {
constructor(userInput = '') {
this._data = userInput
}
get data() {
return this._data
}
isValid() {
if (!Number.isInteger(this._data)) {
return false
}
if (this._data < 0) {
return false
}
if (this._data % 10 === 0) {
return false
}
if (this._data < 10) {
return true
}
let leftPart = this.data
let rightPart = 0
while (leftPart > rightPart) {
// reserve extra space for additional number
rightPart *= 10
// add the most right number
rightPart += leftPart % 10
// remove the most right number from the left-part
leftPart = Math.trunc(leftPart / 10)
}
// compare left and right parts in case left and right part have equal number of digits
// compare left and right parts in case right part has collected the digit in the middle
return leftPart === rightPart || leftPart === Math.trunc(rightPart / 10)
}
}
module.exports = Palindrome