Skip to the content.

3.1 and 3.4 Popcorn and HW Hacks

Javascript and Python Hacks

3.1 Popcorn and HW Hacks

var1 = input("What school do you go to?:")
var2 = input("What sport do you like?:")
var3 = input("What class are you taking right now?:")

my_list = [var1, var2, var3]

my_dict = {
    var1,
    var2,
    var3
}

print(my_list)
print(my_dict)
print("Hi, I go to " + var1 + ", I like " +  var2 + ", and I'm taking " + var3)
['Del Norte', 'soccer', 'CSP']
{'Del Norte', 'soccer', 'CSP'}
Hi, I go to Del Norte, I like soccer, and I'm taking CSP
%%javascript

let fullName = "Jane Smith";
let age = 32;
let email = "jane.smith@example.com";
let hobby = "Mountain Climbing";
let dietaryPreferences = "Vegetarian";

let initials = fullName.slice(0, 2); //first two letters of the name
let randomNum = Math.floor(Math.random() * 900) + 100;
let uniqueID = initials + randomNum;

let userInfo = {
    fullName: fullName,
    age: age,
    email: email,
    hobby: hobby,
    dietaryPreferences: dietaryPreferences,
    uniqueID: uniqueID
};

let output = `
    <h3>Personal Info:</h3>
    <ul>
        <li><b>Full Name:</b> ${userInfo.fullName}</li>
        <li><b>Age:</b> ${userInfo.age}</li>
        <li><b>Email:</b> ${userInfo.email}</li>
        <li><b>Hobby:</b> ${userInfo.hobby}</li>
        <li><b>Dietary Preferences:</b> ${userInfo.dietaryPreferences}</li>
        <li><b>Unique ID:</b> ${userInfo.uniqueID}</li>
    </ul>
`;

element.html(output);
<IPython.core.display.Javascript object>

3.4 Popcorn and HW Hacks

string1 = "I am taking CSP"
string2 = "This is the second sentence"

print("String 1:", string1)
length1 = len(string1)
print("Length of String 1:", length1)

def count_vowels(text):
    vowels = "aeiouAEIOU"
    return sum(1 for char in text if char in vowels)

print("Vowel Count in String 1:", count_vowels(string1))

def avg_word_length(text):
    words = text.split()
    return sum(len(word) for word in words) / len(words) if words else 0

print("Average Word Length in String 1:", avg_word_length(string1))

def is_palindrome(text):
    cleaned = "".join(text.lower().split())
    return cleaned == cleaned[::-1]

print("Is String 1 a Palindrome?", is_palindrome(string1))

print("\nString 2:", string2)
length2 = len(string2)
print("Length of String 2:", length2)
print("Vowel Count in String 2:", count_vowels(string2))
print("Average Word Length in String 2:", avg_word_length(string2))
print("Is String 2 a Palindrome?", is_palindrome(string2))
String 1: I am taking CSP
Length of String 1: 15
Vowel Count in String 1: 4
Average Word Length in String 1: 3.0
Is String 1 a Palindrome? False

String 2: This is the second sentence
Length of String 2: 27
Vowel Count in String 2: 8
Average Word Length in String 2: 4.6
Is String 2 a Palindrome? False
%%javascript

let formHTML = `
    <h3>Password Validator</h3>
    <label for="password">Enter Password:</label>
    <input type="password" id="password" name="password">
    <button id="validateBtn">Validate Password</button>
    <p id="result"></p>
`;

element.append(formHTML);

function passwordValidator(password) {
    if (password.length < 8) {
        return "Password too short. Must be at least 8 characters.";
    }
    if (password === password.toLowerCase() || password === password.toUpperCase()) {
        return "Password must contain both uppercase and lowercase letters.";
    }
    if (!/\d/.test(password)) {
        return "Password must contain at least one number.";
    }

    // Optional
    password = password.replace("123", "abc");

    let customizedPassword = password.split(" ").join("-");

    return `Password is valid! Here’s a fun version: ${customizedPassword}`;
}

document.getElementById("validateBtn").addEventListener("click", function() {
    let password = document.getElementById("password").value;
    let result = passwordValidator(password);
    document.getElementById("result").innerText = result;
});
<IPython.core.display.Javascript object>