Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions starter-code/basic-algorithms.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,82 @@
// Names and Input

var hacker1 = "Laura";
console.log("The driver's name is " + hacker1);

var hacker2 = "Régis";
console.log("The navigator's name is " + hacker2);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can interact with the user by asking his name dynamically with the prompt command :

var hacker2 = prompt("What is the navigator's name?");
console.log("The navigator's name is " + hacker2);


hacker1Up = hacker1.toUpperCase();

var hacker1Array1 = "";

console.log(hacker1Array1);

var hacker1Array2 = "";

console.log(hacker1Array2);

var promptEndroit = "";
var promptEnvers = "";

promptEndroit = prompt("Give me a string");

var subSections = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." ;

var countWords = subSections.split(" ");

var countEt = "" ;


//Conditionals

if (hacker1.length === hacker2.length){
console.log("wow, you both got equally long names, " + hacker1.length + " characters!!");
}
else if (hacker1.length > hacker2.length){
console.log("The Driver has the longest name, it has " + hacker1.length + " characters");
}
else {
console.log("Yo, navigator got the longest name, it has " + hacker2.length + " characters");
}

for (i=0; i < hacker1.length; i++){
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to always use the "var" keyword when you declare a new variable, otherwise you might envcounter bugs in you code!

for (var i=0; i < hacker1.length; i++){
...
}

hacker1Array1 += hacker1Up[i] + " " ;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good logic on this loop and on the one underneath ! You just forgot to do a console.log of hacker1Array1 and hacker1Array2 after each loop to print the result.(Also, these are not technically arrays, they are still strings :) )


for (i=hacker1.length-1;i >= 0; i--){
hacker1Array2 += hacker1[i];
}

if (hacker1[0] > hacker2[0]){
console.log("Yo, the navigator goes first definitely");
}
else if (hacker1[0] < hacker2[0]){
console.log("The driver's name goes first");
}
else {
console.log("What?! You both got the same name?");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are only comparing the first letter of each name, what if both name start with the same letter but are different names ? (like Niccolò and Nicolas :) )
you can actually compare strings alphabetical order by comparing them directly with the "<" sign :

if (hacker1 > hacker2) {
  console.log("Yo, the navigator goes first definitely");
} else if (hacker1 < hacker2) {
  console.log("The driver's name goes first");
} else {
  console.log("What?! You both got the same name?");
}

You can check the fist answer here to understand how it works : https://stackoverflow.com/questions/10198257/comparing-2-strings-alphabetically-for-sorting-purposes


for (i=promptEndroit.length-1;i >= 0; i--){
promptEnvers += promptEndroit[i];
}

if (promptEndroit === promptEnvers){
console.log("It is a palindrome !");
}
else {
console.log("It is not a palindrome !");
}
Copy link
Copy Markdown

@ta-web-paris ta-web-paris Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, your palindrom code works well !! Try to keep it a bit more organised by putting all of the pieces for this bit of logic together. Also, you dont need to initialize your promptEndroit var with an empty string before asking an input to the user :

var promptEndroit = prompt("Give me a string");
var promptEnvers = "";
for ( var i = promptEndroit.length - 1; i >= 0; i--) {
  promptEnvers += promptEndroit[i];
}

if (promptEndroit === promptEnvers) {
  console.log("It is a palindrome !");
} else {
  console.log("It is not a palindrome !");
}


// Lorem ipsum generator

console.log("There are " + countWords.length + " words in this string");

for (i=0;i<countWords.length;i++){
if (countWords[i] === "et"){
countEt ++;
}
}

console.log("There are " + countEt + " 'et' in this string.");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again the code works, good job!
You initialised the countEt variable with an empty string up there, but countEt eventually becomes a number. You would be better signaling your intent if you would initialize it with 0 :

var countWords = subSections.split(" ");
var countEt = 0;

console.log("There are " + countWords.length + " words in this string");

for (var i = 0; i < countWords.length; i++) {
  if (countWords[i] === "et") {
    countEt++;
  }
}

console.log(countEt);