Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An exercise designed to learn basic JS algorithms (variables, loops, etc...)

NotificationsYou must be signed in to change notification settings

MonASV/lab-javascript-basic-algorithms

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 

Repository files navigation

logo_ironhack_blue 7

LAB | JS Basic Algorithms

Welcome to your first JavaScript lab at Ironhack!

This exercise aims to familiarize you with the primitive data structures in JavaScript, which we have covered in class. Feel free to reference lesson materials, and don't limit yourself; be curious and use Google to explore multiple solutions.


lab-js-basic-algorithms-code-collaboration-gif


Learning Goals

This exercise allows you to practice and apply the concepts and techniques taught in class.

Upon completion of this exercise, you will be able to:

  • Declare variables usingconst andlet keywords and use them to store values.
  • Assign values to variables using assignment operators (= ,+= ,-= , etc.).
  • Use conditional statements (if,else if,else) and logical operators (AND, OR, NOT) to control the flow of the program.
  • Access and compare string characters, substrings and length.
  • Manipulate strings using basic string methods and proper (toUpperCase(),toLowerCase(), etc.).
  • Compare values using the comparison operators (< ,> ,<= ,>= ,===).
  • Usefor orwhile loops to iterate over strings in normal and reverse order.


Introduction

For thispair-programming activity, we will use aREPL. To make things as simple as possible, we are going to use an in-browser JavaScript REPL that is provided by the browser-based IDE,repl.it.

Ready to start?

Requirements

  • Fork this repo

  • Clone this repo

  • Go torepl.it and create an account (or log in if you have one)

  • Create a new reply by clicking on+ Create Repl

  • Type this in theCode Editor (left panel)

    console.log("I'm ready!");
  • Pressrun ►

  • If you can see the message in the right side panel, you are really ready!

    lab-js-basic-algorithms-replit-ready

  • When you are done completely, or at any point after the first iteration, copy your code into theindex.js file and follow the steps for submission.

Submission

Upon completion, run the following commands:

git add.git commit -m"done"git push origin master

Create a Pull Request so that the TAs can check your work.

You should make a PR (Pull Request) when you make any significant change. You shouldn't have to wait until you're completely done with this or any other exercise to make the PR. After you do the first PR, any other time you push the changes (following the previous three steps), your change will appear automatically on the PR and your TAs will be able to check it.

Instructions

Iteration 1: Names and Input

1.1 Create a variablehacker1 with the driver's name.
1.2 Print"The driver's name is XXXX".
1.3 Create a variablehacker2 with the navigator's name.
1.4 Print"The navigator's name is YYYY".

Iteration 2: Conditionals

2.1. Depending on which nameis longer, print:
-The driver has the longest name, it has XX characters. or
-It seems that the navigator has the longest name, it has XX characters. or
-Wow, you both have equally long names, XX characters!.

Iteration 3: Loops

3.1 Print the characters of the driver's name, separated by space, andin capital letters, i.e.,"J O H N".

3.2 Print all the characters of the navigator's name in reverse order, i.e.,"nhoJ".

3.3 Depending on thelexicographic order of the strings, print:

  • The driver's name goes first.
  • Yo, the navigator goes first, definitely.
  • What?! You both have the same name?

Bonus Time!

Bonus 1:

Go to thelorem ipsum generator website and:

  • Generate 3 paragraphs. Store the text in a new string variable namedlongText.
  • Make your program count the number of words in the string.
  • Make your program count the number of times the Latin wordet appears.

Bonus 2:

Create a new variable,phraseToCheck, containing some string value. Write a code to check if the value assigned to this variable is aPalindrome. Here are some examples of palindromes:

  • "A man, a plan, a canal, Panama!"
  • "Amor, Roma"
  • "race car"
  • "stack cats"
  • "step on no pets"
  • "taco cat"
  • "put it up"
  • "Was it a car or a cat I saw?" and "No 'x' in Nixon".

IMPORTANT: If you use Google to help you to find a solution to this iteration, you might run into some advanced solutions that use string or array methods (such asjoin(),reverse(), etc.). However, we want you to apply your current knowledge and try to come up with a solution by just using thefor loop andif-else statements with somebreak andcontinue.

Happy coding! ❤️

Extra Resources


FAQs


I am stuck in the exercise and don't know how to solve the problem or where to start.

If you are stuck in your code and don't know how to solve the problem or where to start, you should take a step back and try to form a clear question about the specific issue you are facing. This will help you narrow down the problem and come up with potential solutions.

For example, is it a concept that you don't understand, or are you receiving an error message that you don't know how to fix? It is usually helpful to try to state the problem as clearly as possible, including any error messages you are receiving. This can help you communicate the issue to others and potentially get help from classmates or online resources.

Once you have a clear understanding of the problem, you will be able to start working toward the solution.

Back to top

How do you find a length of a string in JavaScript?

To find the length of a string, you can use thelength property. Here is an example:

conststr="Hello, world!"";console.log(str.length);// 13

Thelength property returns the number of characters in the string, including spaces and special characters.

Back to top

How do I loop over a string?

Here is an example of using afor loop to loop over a string:

letstr="ironhack";for(leti=0;i<str.length;i++){console.log(str[i]);}

This code will iterate over each character in thestr string. The loop will run for as many iterations as there are characters in the string.On each iteration, the loop will log the current character to the console.

Back to top

How do I check if a substring exists in a given string?

You can use theincludes() method to check if a substring exists in a given string.

This method returns a boolean value (true orfalse) indicating whether the string it is called on includes the substring specified as an argument.

Example:

letstr="hello world";console.log(str.includes("hello"));// trueconsole.log(str.includes("world"));// trueconsole.log(str.includes("bye"));// false

You can also use theindexOf() method, which returns the index of the first occurrence of the substring within the string, or -1 if the substring is not found.

Example:

letstr="hello world";console.log(str.indexOf("h"));// 0console.log(str.indexOf("world"));// 6console.log(str.indexOf("bye"));// -1

Back to top

How do I convert a string to capital or lowercase letters?

Uppercase

To convert a string touppercase letters, use thetoUpperCase() method. The methodtoUpperCase() returns a new string with all the characters in uppercase.

Example:

letstr="ironhack";console.log(str.toUpperCase());// "IRONHACK"

Lowercase

To convert a string to alllowercase letters, you can use thetoLowerCase() method. This method returns a new string with all the characters in lowercase.

Example:

letstr="IRONHACK";console.log(str.toLowerCase());// "ironhack"

It's important to note that methodstoUpperCase() andtoLowerCase() do not modify the original string. They return a new string that has been converted to the desired case.

Back to top

How do I reverse a string?

You can use afor loop to iterate over the characters of the string and add them to a new string in reverse order.

Example:

letstr="drawer";letreversed="";for(leti=str.length-1;i>=0;i--){reversed+=str[i];}console.log(reversed);// "reward"

The above example uses afor loop to iterate over the characters of thestr string in reverse order, starting at the last character and ending at the first character. On each iteration, it adds the current character to thereversed string.

Back to top

How do I create a multi-line string in JavaScript?

To create a multi-line string in JavaScript, you must use template literals. Template literals are string literals denoted with backticks (`). They allow you to embed expressions inside string values and create strings that span multiple lines.

Example:

letstr=`This is anexample of amulti-line string.`;console.log(str);

Back to top

I am unable to push changes to the repository. What should I do?

There are a couple of possible reasons why you may be unable topush changes to a Git repository:

  1. You have not committed your changes: Before you can push your changes to the repository, you need to commit them using thegit commit command. Make sure you have committed your changes and try pushing again. To do this, run the following terminal commands from the project folder:
git add.git commit -m"Your commit message"git push
  1. You do not have permission to push to the repository: If you have cloned the repository directly from the main Ironhack repository without making aFork first, you do not have write access to the repository.To check which remote repository you have cloned, run the following terminal command from the project folder:
git remote -v

If the link shown is the same as the main Ironhack repository, you will need to fork the repository to your GitHub account first and then clone your fork to your local machine to be able to push the changes.

Note: You should make a copy of your local code to avoid losing it in the process.

Back to top

About

An exercise designed to learn basic JS algorithms (variables, loops, etc...)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript100.0%

[8]ページ先頭

©2009-2025 Movatter.jp