Skip to content

Instantly share code, notes, and snippets.

@jelenajukic
Created January 23, 2019 12:21
Show Gist options
  • Save jelenajukic/b29fb16d68150b94b3e640202c47f98f to your computer and use it in GitHub Desktop.
Save jelenajukic/b29fb16d68150b94b3e640202c47f98f to your computer and use it in GitHub Desktop.
PrimaryPrimarySampler created by jelenajukic - https://repl.it/@jelenajukic/PrimaryPrimarySampler
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script src="script.js"></script>
</body>
</html>
//function which will work with driver's and navigator's namaes
function compareNames(){
var hacker1="Igor"; //Driver's name is stored in hacker1 variable
console.log("Drivers name is " + hacker1);
// in a prompt window user will be able to insert his.her name
var hacker2=window.prompt("What is your name ", "Type your name here");
console.log ("Navigator's name is " + hacker2);
//if-else if -else statement to check length of the names. I used length property of string as a condition in statement.
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");
}
//Print all the characters of the driver's name, separated by a space and in capitals ie. "J O H N"!!!. I used split method on string to get 1D array. Elements of the array are string characters and I can now easely manipulate with them.
var arrHacker1=hacker1.split("");
var arrHacker2=hacker2.split("");
console.log(arrHacker1.join(" ").toUpperCase()); //I call join method on array with " "(space) separator. It returns string on which I can call toUpperCase() method
console.log(arrHacker2.reverse().join("")); //reverse method I can call on array and it returns array with elements in oposite order than in original array. Now I can call join on reversed array with no-separator.
//This part of the function will do lexicographic comparation of strings using localeCompare method which takes one of the strings for argument and on the second one is called (reference). only in the case it returns zero as result, two strings are equal. I created if-else if-else statement to separate all conditions.
var compare=hacker1.localeCompare(hacker2);
if (compare==1){
console.log("The driver's name goes first")
}else if (compare==0){
console.log("What?! You both got the same name?");
} else {
console.log("Yo, the navigator goes first definitely");
}
}
//-------------------BONUS 1------------------------------------//
// to find out if a string is a palindrom, I compared two strings, original with reversed. If two of them are same then string is a palindrom.
function palindromesChecker(){
var userString=window.prompt("Type a string: ", "Type your string here");
// var userString1=userString.replace(/['?".,\/#!$%\^&\*;:{}=\-_`~() ]/g,"").toUpperCase();
var userString1=userString.toUpperCase().replace(/[^A-Za-z0-9]+/g,""); // first i cleaned up my string from anything which is not letter or number. This is now my original string which i will compare with reversed version of it.
var userStringArrRev=userString1.split(""); // I made array from original string (cleaned-out string). This array I will use to reverse original string
//just want to see in console what i made from string after it is reversed :)
console.log(userStringArrRev.reverse()); //reverse () is called on array
//after i clean it and reverse it, I need to compare it with original one.
if (userString1==userStringArrRev.join("")){
console.log("Palindrom!!!")
} else {
console.log("Sorry, but this is not palindrom!!!")
}
}
//-------------------- BONUS 2 ----------------------//
//This function will count number of words in string made from paragraphs. It takes a string as an argument.
function loremIpsumPara(parragraphs){
//console.log(parragraphs.replace(/\n/, "\\n"));
var arrayOfWords=parragraphs.replace(/\n/, "\\n").split(" "); //First it makes multi line string which is free from new lines. Then it makes array of words. It will make an arrya from string using split method with " " (space) argument.
//console.log(arrayOfWords);
//var arrayOfWords=parragraphs.split(" ");
console.log(arrayOfWords.length); //at the end we can use length method on string to count number of elements in array = number of words in a string.
}
//------------calls to functions-------------------//
//compareNames();
//palindromesChecker();
loremIpsumPara("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla urna nibh, pellentesque a pulvinar ac, porttitor congue nisl. Donec eget consectetur neque. Ut porta, ipsum in aliquam posuere, metus mi cursus urna, congue finibus eros arcu in leo. Quisque aliquam, justo sed volutpat venenatis, enim lacus ultrices dolor, ac pellentesque enim diam nec neque. Fusce sit amet scelerisque ipsum. Phasellus sed quam in nulla lobortis aliquet. Ut commodo leo eget metus blandit ullamcorper. Aliquam velit ipsum, tincidunt ut nisl vel, consectetur mattis eros. Nunc non urna quis dui tincidunt rhoncus. Nullam ac dolor eleifend, viverra massa ut, laoreet risus.\
\
Cras placerat eros et nunc ultricies mollis. Donec ligula velit, malesuada aliquam tellus sit amet, gravida volutpat tortor. Duis vehicula, enim ac sollicitudin tincidunt, odio purus porttitor risus, in sollicitudin tortor enim ut erat. Aenean vel consectetur urna. Nulla facilisi. Duis efficitur tincidunt mollis. Donec blandit mi tincidunt, tempus risus eget, feugiat neque. Integer purus arcu, eleifend quis tortor ac, bibendum euismod urna. Nulla posuere enim eu rhoncus cursus. Praesent imperdiet sapien purus, id egestas nulla convallis sed. Phasellus non pretium metus.\
\
Duis luctus leo et lobortis vulputate. Quisque viverra nisi id diam tempor, a mollis nisi tempus. Nullam id dolor nec nisl pulvinar interdum. Ut efficitur lacus nec sem scelerisque, eu ornare lectus vehicula. Sed at cursus purus. Sed in diam lobortis, aliquet neque vitae, tincidunt massa. Aliquam cursus hendrerit felis. Cras sit amet consequat diam. Phasellus risus dui, tristique sed molestie at, mollis quis nunc. Praesent vel est sagittis, malesuada metus eu, lobortis lectus. Nulla facilisi. Aliquam non auctor leo.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment