Skip to the content.

Javascript_ipynb_2_

%%javascript

// JavaScript Cell: Random number generator and palindrome checker

// Generate a random number between 1 and 100
let randomNumber = Math.floor(Math.random() * 100) + 1;

// Palindrome checker function
function isPalindrome(str) {
    let cleanedStr = str.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
    let reversedStr = cleanedStr.split('').reverse().join('');
    return cleanedStr === reversedStr;
}

// Test palindrome checker
let testString = "A man a plan a canal Panama";

// Output the random number and the palindrome check result
let output = `<h3>Random Number: ${randomNumber}</h3>
<h3>Palindrome Check: ${testString} is ${isPalindrome(testString) ? '' : 'not '}a palindrome.</h3>`;

// Use element to append HTML in Jupyter
element.append(output);

Random Number: 37

Palindrome Check: A man a plan a canal Panama is a palindrome.