Strings and Functions

Some strings below can help find the instances of a certain word to replacing a word in a sentence at random

  • const lionFact = sets the value for lionFact
  • toLowerCase and toUpperCase makes the given sentence in lionFact entirely lowercase or Uppercase
// String Example and Case Conversion
const lionFact = "lions are the only cats that live in groups";

// Convert to lowercase and uppercase
console.log(lionFact.toLowerCase());  // Lowercase
console.log(lionFact.toUpperCase());  // Uppercase

lions are the only cats that live in groups

LIONS ARE THE ONLY CATS THAT LIVE IN GROUPS

Occurrences

  • The .match() method returns all matches of the string “the” in tiger_fact using a regular expression (/the/g).

  • The g flag ensures that the following code finds all occurrences in the string, not just the first one.

// Count occurrences of the word 'groups'
let count = (lionFact.match(/the/g) || []).length;
console.log(count); 

Finding Index

  • index is finding the first letter of a given word in a given const
// Example finding Index of a Substring
console.log(lionFact.indexOf("group"));  // Find the index of the word 'group'

38

The g in group was the 38th character in the lionFact

Replacing Substrings

  • .replace can replace a word in a given const to another word as shown below
// Replacing Substrings
let newLionFact = lionFact.replace('lions', 'giraffes');
console.log(newLionFact);  // Replaced string

// Or replace it directly
lionFact = lionFact.replace('lions', 'giraffes');
console.log(lionFact);  // Replaced string directly

giraffes are the only cats that live in groups

Concatenation

  • Used to join together multiple variables
  • can get messy at times as shown below if not in good format
%%js
// Concatenation
let friend = " Anika ";
let friend1 = " Lara ";
let friend2 = " Nora ";
let friend3 = " Avantika ";

// Bad format
let badFormatSpecies = friend1 + friend + friend2 + friend + friend3 + friend;
console.log(badFormatSpecies);

// Good format with commas
let goodFormatSpecies = `${friend1}${friend}, ${friend2}${friend}, and ${friend3}${friend}`;
console.log(goodFormatSpecies);
<IPython.core.display.Javascript object>

angelfishcatfishpufferfish

angelfish, catfish, pufferfish


Explanation for “Good” and “Bad” formatting

  • fish1 + fish: Concatenates the value of fish1 with fish. This is equivalent to what you can do in Python with fish1 + fish.

  • , ‘ ‘: The comma and space are explicitly added in between fish1 + fish and fish2 + fish to ensure proper formatting and cleaning output.

  • ${} Templateliteral.The ${} syntax is used to embed expressions inside the string. Each ${fish1}${fish} inserts the value of fish1 followed by fish into the string.

Formatted Strings

  • The variable betterFormatSpecies is defined using a template literal (denoted by backticks `).

  • Inside the template literal, the values of the variables fish1, fish2, fish3, and fish are embedded using ${} syntax from before.

  • This string uses commas to separate the fish types and includes the word “and” before the last fish.


<IPython.core.display.Javascript object>

angelfish, catfish, pufferfish are my favorite fish!