Paris - Régis & Laura#225
Conversation
| console.log("The driver's name is " + hacker1); | ||
|
|
||
| var hacker2 = "Régis"; | ||
| console.log("The navigator's name is " + hacker2); |
There was a problem hiding this comment.
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);
| console.log("Yo, navigator got the longest name, it has " + hacker2.length + " characters"); | ||
| } | ||
|
|
||
| for (i=0; i < hacker1.length; i++){ |
There was a problem hiding this comment.
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++){
...
}
|
|
||
| for (i=0; i < hacker1.length; i++){ | ||
| hacker1Array1 += hacker1Up[i] + " " ; | ||
| } |
There was a problem hiding this comment.
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 :) )
| } | ||
| else { | ||
| console.log("What?! You both got the same name?"); | ||
| } |
There was a problem hiding this comment.
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
| } | ||
| else { | ||
| console.log("It is not a palindrome !"); | ||
| } |
There was a problem hiding this comment.
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 !");
}
| } | ||
| } | ||
|
|
||
| console.log("There are " + countEt + " 'et' in this string."); No newline at end of file |
There was a problem hiding this comment.
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);
|
Good job !! Your use of loops is very good and you manage to find solutions to every problems ! |
@jourisque