Skip to content

Commit

Permalink
popcorn hacks for strings + saving strings file
Browse files Browse the repository at this point in the history
  • Loading branch information
leila010 committed Nov 13, 2024
1 parent 2b34e34 commit 703a6ef
Show file tree
Hide file tree
Showing 3 changed files with 696 additions and 0 deletions.
306 changes: 306 additions & 0 deletions _notebooks/Strings/2024-10-30-strings-lesson-4-1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lesson 4.1: Strings\n",
"\n",
"### Exploring strings in JavaScript\n",
"In this lesson, we'll explore how to use and manipulate strings in JavaScript."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Printing Strings\n",
"You can print strings directly or using variables. Here's an example of both:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dog\n",
"cat\n",
"caribou\n"
]
}
],
"source": [
"%%js\n",
"// Directly printing a string\n",
"console.log(\"dog\");\n",
"\n",
"// Using Variables\n",
"const animal = \"cat\";\n",
"const favoriteAnimal = \"caribou\";\n",
"\n",
"console.log(animal); // Output: cat\n",
"console.log(favoriteAnimal); // Output: caribou\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Handling Apostrophes\n",
"If your string contains an apostrophe, make sure to escape it when using single quotes."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I'm eating pizza\n",
"I'm eating lasagna\n"
]
}
],
"source": [
"%%js\n",
"// Escaping apostrophes in strings\n",
"const food2 = 'I\\'m eating pizza';\n",
"const food3 = \"I'm eating lasagna\";\n",
"\n",
"console.log(food2); // Output: I'm eating pizza\n",
"console.log(food3); // Output: I'm eating lasagna\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Strings with Multiple Lines\n",
"You can create multi-line strings using template literals (backticks). Here's an example:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [
{
"data": {
"application/javascript": "// Multi-line string using template literals\nconst riddle = `my favorite animal\nis associated with winter\nand Christmas`;\n\nconsole.log(riddle);\n```\n",
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%js\n",
"// Multi-line string using template literals\n",
"const riddle = `my favorite animal\n",
"is associated with winter\n",
"and Christmas`;\n",
"\n",
"console.log(riddle);\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Length of a String\n",
"You can find the length of a string using the `.length` property."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11\n"
]
}
],
"source": [
"%%js\n",
"// Find the length of a string\n",
"console.log(\"earthmovers\".length); // Output: 11\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Printing Specific Parts of a String\n",
"You can extract specific parts of a string using indexing and slicing. Here are some examples:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"c\n",
"chucks do n\n",
"woodchucks do not act\n",
"dirt more often than not\n"
]
}
],
"source": [
"%%js\n",
"// Extracting specific parts of a string\n",
"const funFact = \"woodchucks do not actually chuck wood, they chuck dirt more often than not\";\n",
"\n",
"// Specific character:\n",
"console.log(funFact[10]); // Output: 'c'\n",
"\n",
"// Range (substring):\n",
"console.log(funFact.slice(5, 15)); // Output: 'chucks do n'\n",
"\n",
"// Slicing:\n",
"console.log(funFact.slice(0, 25)); // Output: 'woodchucks do not act'\n",
"console.log(funFact.slice(35)); // Output: 'dirt more often than not'\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Popcorn Hack - Palindromes\n",
"A palindrome is a word or phrase that reads the same backward as forward. You can create a function to check for palindromes. Here's a function that does that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [
{
"data": {
"application/javascript": "// Function to check if a string is a palindrome\nfunction palindrome(inputStr) {\n // Remove spaces and convert the string to lowercase\n const cleanStr = inputStr.replace(/\\s+/g, '').toLowerCase();\n // Check if the cleaned string is equal to its reverse\n return cleanStr === cleanStr.split('').reverse().join('');\n}\n\nconsole.log(palindrome(\"did you know that sahas spelled backwards is sahas\")); // Output: false\nconsole.log(palindrome(\"hi\")); // Output: false\n```\n",
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%js\n",
"// Function to check if a string is a palindrome\n",
"function palindrome(inputStr) {\n",
" // Remove spaces and convert the string to lowercase\n",
" const cleanStr = inputStr.replace(/\\s+/g, '').toLowerCase();\n",
" // Check if the cleaned string is equal to its reverse\n",
" return cleanStr === cleanStr.split('').reverse().join('');\n",
"}\n",
"\n",
"console.log(palindrome(\"did you know that sahas spelled backwards is sahas\")); // Output: false\n",
"console.log(palindrome(\"hi\")); // Output: false\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [
{
"data": {
"application/javascript": "// Function to check if a string is a palindrome\nfunction palindrome(inputStr) {\n // Remove spaces and convert the string to lowercase\n const cleanStr = inputStr.replace(/\\s+/g, '').toLowerCase();\n // Check if the cleaned string is equal to its reverse\n return cleanStr === cleanStr.split('').reverse().join('');\n}\n\nconsole.log(palindrome(\"A man a plan a canal Panama\")); // Output: true\nconsole.log(palindrome(\"racecar\")); // Output: true\n",
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%js\n",
"// Function to check if a string is a palindrome\n",
"function palindrome(inputStr) {\n",
" // Remove spaces and convert the string to lowercase\n",
" const cleanStr = inputStr.replace(/\\s+/g, '').toLowerCase();\n",
" // Check if the cleaned string is equal to its reverse\n",
" return cleanStr === cleanStr.split('').reverse().join('');\n",
"}\n",
"\n",
"console.log(palindrome(\"A man a plan a canal Panama\")); // Output: true\n",
"console.log(palindrome(\"racecar\")); // Output: true\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 703a6ef

Please sign in to comment.