Check a message's checksum

The sender of a message also sends its checksum. The receiver works the checksum out again: if the two differ, the message changed on the way.

What to build

Read a message and the checksum that came with it, and say whether the message is intact.

Requirements

  • Use the lesson's checksum: start at 0, and for each character set sum = (sum * 31 + code) % 65536, where code is the character's charCodeAt value.
  • Print intact when the numbers match.
  • Otherwise print changed, a space and the checksum you worked out.

Input

Line 1: the message (it may contain spaces). Line 2: the checksum that came with it, a whole number.

Output

intact, or changed followed by the computed checksum.

Constraints

The message has 1 to 200 characters. The checksum is 0 to 65535.

Example

Input:

Pay 500 taka to Rahim
30322

Output:

intact

Why it works

The receiver repeats exactly the sender's calculation, so an unchanged message always gives the same number, and almost any change gives a different one.

Examples

Example 1 · Input
Pay 500 taka to Rahim
30322
Expected output
intact
Example 2 · Input
Pay 900 taka to Rahim
30322
Expected output
changed 61046

Hints

Hint 1

Write checksum(text) first and test it on the lesson's example: "Pay 500 taka to Rahim" gives 30322.

Hint 2

Number(line) turns the second line into a number.

Solution

Show one correct solution (try it yourself first)
function checksum(text) {
  let sum = 0;
  for (let k = 0; k < text.length; k++) sum = (sum * 31 + text.charCodeAt(k)) % 65536;
  return sum;
}
const message = prompt();
const expected = Number(prompt());
const got = checksum(message);
console.log(got === expected ? "intact" : `changed ${got}`);

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: What security protects: the CIA triad →