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
67 changes: 66 additions & 1 deletion starter-code/basic-algorithms.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,72 @@
// Names and Input
//1
let hacker1 = 'Alex';
//2
console.log("The driver's name is " + hacker1);
//3
let hacker2 = prompt("What's your name");
//4
console.log("The navigator's name is " + hacker2);
//Conditionals
//5
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 if (hacker1.length < hacker2.length) {
console.log('Yo, navigator got the longest name, it has ' + hacker2.length + ' characters');
}
//6
console.log(hacker1.toUpperCase().split("").join(" "))
//7
console.log(hacker2.split("").reverse().join(""))

//8

//Conditionals
var compareVariable = hacker1.localeCompare(hacker2);

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


//9
let palindromeString = prompt("Write to know if it's a palindrome").toLowerCase();

let normalString;

for (let i = 0; i < palindromeString.length; i++) {
if (palindromeString[i] !== " " && palindromeString[i] !== ",") {
normalString = normalString + palindromeString[i];
}
}

@ta-web-mad ta-web-mad Apr 29, 2018

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Y cómo harías para quitar las comillas de la última frase? porque la última es palíndrome si quitas las comillas y no evalúas las comillas, y es más, cómo quitarías cualquier símbolo que te pudiese llegar a poner para comprobar el suceso? (Lo que has hecho está bien, pero cómo lo pensarías para que te valiese para más casos?)

Tip: dale un repaso a cómo harías el ejercicio de localeCompare sin usar dicho método

let reverseString = normalString.split(" ").reverse().join();

if (reverseString === normalString) {
console.log("It's a palindrome");
} else {
console.log("It isn't a palindrome");
}
// Lorem ipsum generator
//10
let text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin metus est, gravida vel ullamcorper eget, imperdiet sit amet nulla. Nam faucibus, nisi et venenatis interdum, risus sapien viverra lectus, a laoreet lorem dolor consequat purus. Sed tempor sapien ac tellus tincidunt ullamcorper. Aliquam sollicitudin semper dolor eu tristique. Aliquam aliquam, sem ac lacinia volutpat, tellus felis pellentesque mauris, et consectetur nibh purus non nisi. Etiam faucibus tellus at turpis bibendum fermentum. Mauris nibh orci, tempus a massa at, laoreet faucibus justo. Morbi vitae suscipit nunc. Integer lacinia est et metus congue, id tincidunt leo viverra. Mauris volutpat non nisi nec dapibus. Donec lacinia mauris diam, convallis lacinia ipsum vestibulum sed. Phasellus vulputate et diam non ultricies. Duis sit amet ipsum vel lacus pulvinar facilisis eu sit amet lacus. Donec condimentum arcu erat, ut feugiat odio sodales eu. Pellentesque et gravida sem. In hac habitasse platea dictumst. Curabitur aliquam sed erat nec accumsan. Vivamus vestibulum pharetra est. Suspendisse ut lectus turpis. Nam pulvinar eros eu mauris viverra fringilla. Nunc urna risus, posuere id ipsum eget, finibus viverra urna. Cras sagittis est a libero gravida rhoncus. Vestibulum nec elit at enim dignissim vulputate id non augue. Phasellus sed accumsan erat, nec efficitur tellus. Aenean sem nulla, viverra a posuere vitae, tincidunt id est. Phasellus non porta diam, non pellentesque sapien. Fusce at felis eget leo porta semper. Pellentesque sit amet bibendum elit. Vivamus varius aliquam diam, in vestibulum sapien. Nunc convallis egestas erat, ac porttitor ligula molestie id."

let wordCount = 0;

for (let i = 0; i <= text.length; i++) {
if (text[i] === " ") {
wordCount++;
}
if (i === text.length) {
wordCount++;
}
}
console.log("Number of words: " + wordCount);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No es mala idea ir contando los espacios para saber el número de palabras, pero si ya has utilizado la función split(), por qué no mejor:
nWords = text.split(' ').length

let wordAux = text.split(" ").filter(item => item === 'et');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Está bien y es correcto, mi recomendación es que lo escribas de la siguiente manera por ahora:
var et = text.split(' ').filter(function(item) { return item === 'et' })
No sé cuál es tu conocimiento de js, pero las arrow functions son de ES6 e implican algo más que una semántica más limpia que versiones anteriores de js. Hasta que lleguemos a ver ES6 te recomiendo utilizar 'function()'

console.log("Number of times the latin word et appears: " + wordAux.length)