Skip to content
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
40 changes: 37 additions & 3 deletions basic-algorithms.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
// Names and Input
//variables
var hacker1 = "Samuel"
var hacker2 = window.prompt("what is the navigators name?")


//Conditionals
//console logs
console.log(`The drivers name is ${hacker1}`);
console.log(`The navigators name is ${hacker2)}`)


// Lorem ipsum generator
//Let's get looping
for (var i = 0; i < hacker1.length || hacker2.length; i++){
Copy link
Collaborator

@tawebbcn tawebbcn Oct 22, 2019

Choose a reason for hiding this comment

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

The challenge here was to compare the length of two element. It would have been easier to just compare element1.length and element2.length with no need to use a for loop since you can get the length of a string directly with the ".length()" property. Then you only return the one that is longer (if element1.length < element2.length return element2)

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(`Yo, navigator got the longest name, it has ${hacker2.length} characters`)
} else {}
console.log(`wow, you both got equally long names, ${hacker2.length} characters!!`)
}

//loop drivers name
for(let i = 0; i < hacker1.length; i++){
console.log(`${hacker1[i]}`)
}

//loops driver name backwards
for(let i = hacker.length; i > 0; i--){
Copy link
Collaborator

Choose a reason for hiding this comment

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

here for 'samuel' backwards you'd get 'amuel', missing the first letter. To reach the first letter you need to iterate backwards and set the condition of the for loop (the middle statement) to also equal 0, just like this:

for(let i = hacker.length; i >= 0; i--) You only forgot to add the equal symbol

console.log(`${hacker1[i]}`)
}



//loop on first letter..
for(let i = 0; i < 1; i++){
if (hacker1[0] === "a"){
console.log(`The driver's name goes first`)
} else if (hacker2[0] === "a"){
console.log(`Yo, the navigator goes first definitely`)
} else {}
console.log(`What?! You both got the same name?`)
}