From f1eefe4bede8f71559cf4a04f3c7c53d34118481 Mon Sep 17 00:00:00 2001 From: Sadri Sellami Date: Mon, 7 Oct 2019 16:50:40 +0200 Subject: [PATCH] done --- README.md | 90 ++++++++++++++++++-------------- starter-code/basic-algorithms.js | 86 ++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 38 deletions(-) create mode 100644 starter-code/basic-algorithms.js diff --git a/README.md b/README.md index 7c164efbd..8252a41e2 100644 --- a/README.md +++ b/README.md @@ -1,81 +1,95 @@ -![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) +![Ironhack Logo](https://i.imgur.com/1QgrNNw.png) -# Lab | JS Basic Algorithms +# JS | Basic Algorithms -Welcome to your first bootcamp exercise at Ironhack! +Welcome to your first bootcamp exercise at IronHack! -The goal of this exercise is to get you familiarized with the different data structures we have in JavaScript. +The goal of this exercise is to get you acquainted with the different control structures we have in JavaScript. Ready? ## Introduction -For this **pair-programming** activity we are going to use a [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop). You can find a REPL that runs in the browser for programming languages as JavaScript in [https://repl.it/languages/nodejs](https://repl.it/languages/nodejs) +For this Pair-Programming activity we are going to use a [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop). You can fine a REPL that runs in the browser for programming languages as JavaScript in [repl.it](https://repl.it/) + +We could also type JavaScript code in an editor, and then execute it to see the results in the REPL. Ready to start? ## Requirements -- Go to [repl.it](https://repl.it/languages/nodejs) and create an account (or login if you have one) -- Type this in the *Code Editor* (left panel) +- Go to [repl.it](https://repl.it/languages/javascript) and create an account (or login if you have one) +- Open a new repl.it and make sure the language is ==JavaScript== +- Type this in the ==Code Editor== (left panel) ```javascript - console.log("I'm ready!"); + console.log("I'm Ready!"); ``` - Press `run ►` -- If you can see the message in the right side panel, you are really ready! +- If you can see the message on the JavaScript REPL, you're ready!! - - ![](https://s3-eu-west-1.amazonaws.com/ih-materials/uploads/upload_17f095b9bb4fa4bd1bee1c017c043327.png) + ![](https://i.imgur.com/4TQislb.png) ## Submission -When you are done and you have checked that everything works fine, click on the **Share** button and copy the link from the *Share Link* field. Send this link to your TAs so they can check up your work. - -![](https://s3-eu-west-1.amazonaws.com/ih-materials/uploads/upload_b2aa98f91affe5d4c5f12f216b069184.png) +When you are done and you have checked that everything works fine, go to https://gist.github.com/, create a new [secret gist](https://help.github.com/articles/about-gists/#secret-gists) and send it to your Instructor. + + ![](https://i.imgur.com/ux5em6j.png) +**Remember:** Remember to add the extension of the file `fileName`**.js** in the *"Filename including extension"* input box so you can enjoy [syntax highlighting](https://en.wikipedia.org/wiki/Syntax_highlighting) in your gist -## Instructions +## Exercise -### Iteration 1: Names and Input +### Names and Input - 1.1 Create a variable `hacker1` with the driver's name. +1. Create a variable `hacker1` with the driver's name - 1.2 Print `"The driver's name is XXXX"`. +2. Print `"The driver's name is XXXX"` - 1.3 Create a variable `hacker2` with the navigator's name. +3. Create a variable `hacker2` and [ask the user](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt) for the navigator's name - 1.4 Print `"The navigator's name is YYYY"`. +4. Print `"The navigator's name is YYYY"` -### Iteration 2: Conditionals +### Conditionals - 2.1. Depending on which name [is longer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length), print: - - `The Driver has the longest name, it has XX characters` or - - `Yo, navigator got the longest name, it has XX characters` or - - `Wow, you both got equally long names, XX characters!` +5. Depending on which name [is longer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length), print: + - `The Driver has the longest name, it has XX characters` or + - `Yo, navigator got the longest name, it has XX characters` or + - `wow, you both got equally long names, XX characters!!` -### Iteration 3: Loops +### Loops - 3.1 Print all the characters of the driver's name, separated by a space and [in capitals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) - i.e. `"J O H N"` +6. Print all the characters of the driver's name, separated by a space and [in capitals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) + ie. `"J O H N"` - 3.2 Print all the characters of the navigator's name, in reverse order. - i.e. `"nhoJ"` +7. Print all the characters of the navigator's name, in reverse order. + ie. `"nhoJ"` - 3.3 Depending on the [lexicographic order](https://en.wikipedia.org/wiki/Lexicographical_order) of the strings, print: - - `The driver's name goes first.` - - `Yo, the navigator goes first definitely.` - - `What?! You both got the same name?` +8. Depending on the [lexicographic order](https://en.wikipedia.org/wiki/Lexicographical_order) of the strings, print: + - `The driver's name goes first` + - `Yo, the navigator goes first definitely` + - `What?! You both got the same name?` ### Bonus Time! -Go to [lorem ipsum generator](http://www.lipsum.com/) and: - - Generate 3 paragraphs. Store the text in a variable type of string. - - Make your program count the number of words in the string. - - Make your program count the number of times the Latin word [`et`](https://en.wiktionary.org/wiki/et#Latin) appears. +9. Ask the user for a new string and check if it's a [Palindrome](https://en.wikipedia.org/wiki/Palindrome). 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". + +10. Go to [lorem ipsum generator](http://www.lipsum.com/) and: + - Generate 3 parragraphs. Store the text in a String + - Make your program count the number of words in the string + - Make your program count the number of times the latin word [`et`](https://en.wiktionary.org/wiki/et#Latin)appears ## Extra Resources +- [prompt() - MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt) | Ask user for input - [String - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) - [if - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) - [while - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while) diff --git a/starter-code/basic-algorithms.js b/starter-code/basic-algorithms.js new file mode 100644 index 000000000..0150bb3de --- /dev/null +++ b/starter-code/basic-algorithms.js @@ -0,0 +1,86 @@ +console.clear; +var driver = "sadri"; +console.log("the driver name is " + driver); +var navigatore = prompt("enter Navigator Name"); +console.log("the navigator's name is " + navigatore); + +if (driver.length > navigatore.length) { + console.log( + "the driver has the longest name, it has" + driver.length + "characters" + ); +} else if (driver.length < navigatore.length) { + console.log( + "the navigator has the longest name, it has" + + navigatore.length + + "characters" + ); +} else { + console.log( + "wow! you both got equally long names" + navigatore.length + "carachters" + ); +} +result = ""; +result2 = ""; +for (let i = 0; i < driver.length; i++) { + result = result + " " + driver[i]; + result2 = result2 + driver[driver.length - i - 1]; +} +console.log(result); +console.log(result2); + +lexico = driver.localeCompare(navigatore); +switch (lexico) { + case 1: + console.log("Navigator is first"); + break; + case -1: + console.log("yo the driver goes first "); + break; + default: + console.log("what! you have the same name"); + break; +} + +var phrase = prompt("enter your phrase"); +console.log("Print " + phrase); +phraseNoSpace = phrase + .replace(/[., \/#!$%\^&\*;:{}=\-_`~()]/g, "") + .toLowerCase(); +console.log(phraseNoSpace); +let i = 0; +while ( + phraseNoSpace[i] === phraseNoSpace[phraseNoSpace.length - i - 1] && + i < (phraseNoSpace.length - 1) / 2 +) { + i++; +} +console.log(i); +if (i === Math.floor(phraseNoSpace.length / 2)) { + console.log("this is palindrome"); +} else { + console.log("not palindrome"); +} +var loremString = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean aliquet est nec lacus rhoncus luctus. Praesent eu vulputate sem. Sed ut aliquet justo. Praesent ex ligula, consequat scelerisque sodales sodales, maximus id purus. Aliquam id pellentesque est, et tempor ipsum. Duis quis neque vitae justo ultrices ultricies. Morbi vitae vulputate libero. Sed ut ornare magna, sed feugiat ligula. Phasellus interdum mi ac libero iaculis tristique Aliquam finibus ante sed augue vehicula bibendum. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla facilisi. Vestibulum lacus velit, sollicitudin id blandit at, pharetra pharetra tellus. Aenean rutrum, velit tincidunt placerat interdum, ex sapien lobortis eros, sit amet volutpat arcu justo sagittis magna. Proin a gravida elit. Ut id tempor leo. Nulla eu fringilla ligula, quis volutpat mauris. Maecenas sit amet maximus dui, eu sagittis ligula. Donec auctor facilisis nibh, et posuere nisl vestibulum eleifend. Vivamus libero ante, ultricies sit amet volutpat eget, dignissim a metus. Curabitur elementum, felis quis semper fermentum, est purus ullamcorper enim, vel accumsan lacus metus ac neque. In quis ipsum sodales, ullamcorper leo eget, sollicitudin tortor. Cras cursus placerat laoreet. Quisque semper vehicula tempor. Aenean ac velit urna. Cras a velit magna. Donec erat mi, mattis semper dui non, aliquet venenatis lectus. Nam tempor est in aliquet varius. Donec nec facilisis arcu. Vestibulum eget justo ligula. Fusce sollicitudin maximus quam eu feugiat. Donec dictum id metus vel tristique. Donec et sapien viverra tellus ultrices finibus. Vestibulum elementum rhoncus metus, sed hendrerit enim dapibus a."; + +var wordCount = 1; +var charCount = 0; + +while (charCount < loremString.length - 1) { + if (loremString[charCount] === " ") { + wordCount++; + } + charCount++; +} +console.log(wordCount); + +var wordcountET = 0; +var charcountET = 0; + +while (charcountET < loremString.length - 5) { + if (loremString.substring(charcountET, charcountET + 4) === " et ") { + wordcountET++; + } + charcountET++; +} +console.log(wordcountET);