-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Module1_Assignment_Boyuan_Amsterdam_pt0107 #657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,129 @@ | ||
| // Iteration 1: Names and Input | ||
| //1.1 create the hacker1 variable | ||
| let hacker1 = 'Tom'; | ||
| //1.2 print the driver's name | ||
| console.log(`This driver's name is ${hacker1}.`); | ||
| //1.3 create the hacker2 variable | ||
| let hacker2 = 'Jerry'; | ||
| //1.4 print the navigator's name | ||
| console.log(`The navigator's name is ${hacker2}.`); | ||
|
|
||
|
|
||
| // Iteration 2: Conditionals | ||
| //2.1 print the string depending on the length of their names. | ||
| if (hacker1.length > hacker2.length) { | ||
| console.log(`The driver has the longest name, it has ${hacker1.length} characters.`) | ||
|
|
||
| } else if (hacker1.length < hacker2.length) { | ||
| console.log(`It seems that the navigator has the longest name, it has ${hacker2.length} characters.`) | ||
| } else { | ||
| console.log(`Wow, you both have equally long names, ${hacker1.length} characters!`) | ||
| } | ||
|
|
||
| // Iteration 3: Loops | ||
|
|
||
| // let hacker1Arr =[]; | ||
| // for (let i=0; i<hacker1.length; i++) { | ||
| // hacker1Arr.push(hacker1[i].toUpperCase()); | ||
| // } | ||
| // console.log(hacker1Arr.join(' ')) | ||
|
|
||
| //3.1 create new variable called newHacker1 and store the character separated by space and in Capitals | ||
| let newHacker1 = ''; | ||
| for (let i = 0; i<hacker1.length; i++) { | ||
| newHacker1 += hacker1[i].toUpperCase()+' '; | ||
| } | ||
| console.log(newHacker1); | ||
|
|
||
|
|
||
|
|
||
| // let hacker2Arr =[]; | ||
| // for (let i = hacker2.length-1; i>-1; i--) { | ||
| // hacker2Arr.push(hacker2[i]); | ||
| // } | ||
| // console.log(hacker2Arr.join('')); | ||
|
|
||
|
|
||
| //3.2 create the new variable called newHacker2 and store the character in reverse order. | ||
| let newHacker2 = ''; | ||
| for (let i = hacker2.length-1; i > -1; i--) { | ||
| newHacker2 += hacker2[i]; | ||
| } | ||
| console.log(newHacker2); | ||
|
|
||
| //3.3 use localeCompare method to compare two string by lexicographic order. | ||
| if (hacker1.localeCompare(hacker2) < 0) { | ||
| console.log('The driver\'s name goes first.'); | ||
| } else if (hacker1.localeCompare(hacker2) > 0) { | ||
| console.log('Yo, the navigator goes first definitely.'); | ||
| } else { | ||
| console.log('What?! You both have the same name?'); | ||
| } | ||
|
|
||
| //#### Bonus1 ! | ||
|
|
||
| //B1.1 use mode_modules to create lorem ipsum in JS | ||
| let loremIpsum = require('lorem-ipsum'); | ||
| let outPut = loremIpsum({ | ||
| count: 3 // Number of words, sentences, or paragraphs to generate. | ||
| , units: 'paragraphs' // Generate words, sentences, or paragraphs. | ||
| , sentenceLowerBound: 5 // Minimum words per sentence. | ||
| , sentenceUpperBound: 15 // Maximum words per sentence. | ||
| , paragraphLowerBound: 3 // Minimum sentences per paragraph. | ||
| , paragraphUpperBound: 7 // Maximum sentences per paragraph. | ||
| , format: 'plain' // Plain text or html | ||
| , random: Math.random // A PRNG function. Uses Math.random by default | ||
| }); | ||
| console.log(outPut); | ||
|
|
||
| //B1.2 use the space string :' ' to check how many words | ||
| let wordNum = 3; | ||
| for (let i = 0; i<outPut.length; i++) { | ||
| if(outPut[i]===' ') { | ||
| wordNum++ | ||
| } | ||
| } | ||
| console.log(wordNum); | ||
|
|
||
| //B1.3 use the 'et' string and substr method to test how many 'et' word we have | ||
| let etNum = 0; | ||
| for (let i=0; i < outPut.length; i++) { | ||
| if(outPut.substr(i,4)===' et '|| outPut.substr(i,4)===' et.') { | ||
| etNum++; | ||
| } | ||
| } | ||
| for(let i=0; i<outPut.length; i++) { | ||
| if(outPut.substr(i,3)==='Et '){ | ||
| etNum++; | ||
| } | ||
| } | ||
| console.log(etNum); | ||
|
|
||
| //#### bonus 2 | ||
|
|
||
| // B2.1 clean the phrase string to keep the word left only. | ||
| let phraseToCheck = 'hsu sosn ssos .,ndu'; | ||
| phraseToCheck = phraseToCheck.replace(/[^\w]|_/g,''); | ||
| //console.log(phraseToCheck); | ||
|
|
||
| //B2.2 make a new variable to change the character order in reverse. | ||
| let newPhrase = ''; | ||
| for(let i =phraseToCheck.length-1; i > -1; i--) { | ||
| newPhrase += phraseToCheck[i] | ||
| } | ||
| //console.log(newPhrase); | ||
|
|
||
| //B2.3 test if the phrase is a Palindrome phrase. | ||
| if(newPhrase === phraseToCheck) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use ".toLowerCase()" in your if statement in case there are uppercase letters in phraseToCheck. |
||
| console.log('This phrase is a Palindrome phrase.') | ||
| } else { | ||
| console.log('This phrase is not a Palindrome phrase.') | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // Iteration 3: Loops | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also use another "| || operator in the first for loop and remove the second for loop.