Skip to the content.

3.4_teamteach_ipynb_2_

<script>
    // Example: Song lyrics
    let lyrics = "Row your boat gently down the stream. Merrily merrily merrily merrily, life is but a dream.";
    let songTitle = "Row";
    
    // Count how many times the title appears
    let titleCount = lyrics.split(songTitle).length - 1;
    console.log(`The title "${songTitle}" appears ${titleCount} times.`);
    element.append(`The title "${songTitle}" appears ${titleCount} times.<br>`);
    
    // Find the index of the 50th word
    let words = lyrics.split(" ");
    if (words.length >= 50) {
        console.log("The 50th word is:", words[49]);
        element.append("The 50th word is: " + words[49] + "<br>");
    } else {
        console.log("There are less than 50 words.");
    }
    
    // Replace the first verse with the last verse
    let verses = lyrics.split(".");
    let modifiedLyrics = lyrics.replace(verses[0], verses[verses.length - 1]);
    console.log("Modified Lyrics: ", modifiedLyrics);
    
    // Concatenate two verses
    let newSong = verses[1].trim() + " " + verses[2].trim();
    console.log("New Song: ", newSong);
    </script>
    

Lesson 3.4.3 (Popcorn) - java

  • Hack: Concat a flower with an animal to make a new creature and create a short story
<script>
    // Concatenating flower and animal
    let flower = "Rose";
    let animal = "Tiger";
    let newCreature = flower + animal;
    
    console.log("New Creature: ", newCreature);
    element.append("New Creature: " + newCreature + "<br>");
    
    // Short Story
    let story = `Once upon a time, there was a ${newCreature} that roamed the forest. \n
    Everyone feared the ${newCreature} because it had the beauty of a rose and the strength of a tiger. \n
    One day, a brave child said, "I'll be friends with the ${newCreature}!" And from that day, the forest was peaceful.`;
    
    console.log("Short Story:\n", story);
    element.append("Short Story:<br>" + story.replace(/\n/g, "<br>"));
    </script>
    

Lesson 3.4.4 (Main) - Java

  • hack: Create a text analyzer that accepts user input
<script>
    // Text Analyzer
    function textAnalyzer(inputText) {
        // Split text into words
        let words = inputText.split(" ");
        console.log("Total number of words:", words.length);
        element.append("Total number of words: " + words.length + "<br>");
    
        // Replace first word with last
        words[0] = words[words.length - 1];
        let modifiedText = words.join(" ");
        console.log("Modified Text: ", modifiedText);
        element.append("Modified Text: " + modifiedText + "<br>");
    
        // User can replace specific words (extra input system)
        let replacedText = modifiedText.replace("dream", "reality");
        console.log("Replaced Word Text: ", replacedText);
        element.append("Replaced Word Text: " + replacedText + "<br>");
    }
    
    // Example input
    let userInput = "Life is but a dream, and the journey never ends.";
    textAnalyzer(userInput);
    </script>