Skip to content

FTWD MIA Rafael Guasp lab-javascript-basic-algorithms - #106

Closed
rguasp wants to merge 1 commit into
ironhack-labs:masterfrom
rguasp:master
Closed

FTWD MIA Rafael Guasp lab-javascript-basic-algorithms#106
rguasp wants to merge 1 commit into
ironhack-labs:masterfrom
rguasp:master

Conversation

@rguasp

@rguasp rguasp commented Apr 2, 2018

Copy link
Copy Markdown

No description provided.

@ianizaguirre

Copy link
Copy Markdown

Hi, here is some Feedback.

  1. Here is some feedback for the code block below. There is no purpose for var sameLength = false; it does not do anything in the code, so I don't know why it was made?
var hacker1 = "Alexander";
var hacker2 = "Bill";
var sameLength = false;

if(hacker1.length > hacker2.length){
  console.log("The Driver has the longest name, it has " + hacker1.length + " characters");
}
else if(hacker2.length > hacker1.length){
  console.log("Yo, navigator got the longest name, it has "+ hacker2.length +" characters");
  
} 
else {
  sameLength = true;
  console.log("wow, you both got equally long names, "+ hacker1.length +" characters!!");
}
  • You could have just written the above code like this and got the same result:
var hacker1 = "Alexander";
var hacker2 = "Bill";

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

if(hacker1.length <= hacker2.length){
  shortest = hacker1.length;
}
else{
  shortest = hacker2.length;
}

for(var i = 0; i < shortest ; i++){
  if (hacker1[i].toUpperCase() < hacker2[i].toUpperCase()){
    same = false;
    console.log("The driver's name goes first");
    break;
  }
  else if(hacker2[i].toUpperCase() < hacker1[i].toUpperCase()){
    same = false;
    console.log("Yo, the navigator goes first definitely");
    break;
  }
}

if (same === true){
  console.log("What?! You both got the same name");
}
  • The rest of that above code should have been written inside of a function. Having If/Else and Loops outside of a function or code block should be avoided since we want to avoid placing things in the Global Scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants