Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.
- function canIVote(age){
- if(age < 18){
- console.log("You cannot vote now.");
- return false;
- }
- else{
- console.log("You can vote, you are up to 18 years old.");
- return true;
- }
- }
- console.log(canIVote(19)) // Should print true
- 2.
- function agreeOrDisagree(string1, string2){
- return string1 === string2 ? "You agree!" : "You disagree!";
- }
- console.log(agreeOrDisagree("yep", "yep")) // Prints "You agree!"
- 3.
- const lifePhase = (age) => {
- if(age < 0 || age > 140){
- return "This is not a valid age";
- }
- else if(age > 0 && age <= 3){
- return "baby";
- }
- else if(age >= 4 && age <= 12){
- return "child";
- }
- else if(age >= 13 && age <= 19){
- return "teen";
- }
- else if(age >= 20 && age <= 64){
- return "adult";
- }
- else if(age >= 65 && age <= 140){
- return "senior citizen";
- }
- }
- console.log(lifePhase(3.5)) //should print 'child'
- 4.
- function finalGrade(num1, num2, num3){
- if(num1 < 0 || num1 > 100 || num2 < 0 || num2 > 100 || num3 < 0 || num3 > 100){
- return "You have entered an invalid grade."
- }
- const average = (num1 + num2 + num3) / 3;
- console.log("Average: " + average);
- if(average >= 0 && average <= 59){
- return "F";
- }
- if(average >= 60 && average <= 69){
- return "D";
- }
- if(average >= 70 && average <= 79){
- return "C";
- }
- if(average >= 80 && average <= 89){
- return "B";
- }
- if(average >= 90 && average <= 100){
- return "A";
- }
- }
- console.log(finalGrade(99, 92, 95)) // Should print 'A'
- 5.
- const reportingForDuty = (rank, lastName) => (rank + " " + lastName + " reporting for duty!");
- console.log(reportingForDuty('Private', 'Fido')) // Should return 'Private Fido reporting for duty!'
- 6.
- const rollTheDice = () => {
- // Math.random() gives us a random number from 0 up to, but not including, 1
- // We multiplied that by 6 to get a number between 0 and up to, but not including, 6
- // But since we actually wanted numbers from 1 to 6, inclusive, we added 1
- let die1 = Math.floor(Math.random() * 6 + 1);
- let die2 = Math.floor(Math.random() * 6 + 1);
- return die1 + die2
- }
- 7.
- function calculateWeight(earthWeight, planet){
- switch(planet){
- case "Mercury":
- return 0.378 * earthWeight;
- break; // Not necessary, because of return-statement above
- case "Venus":
- return 0.907 * earthWeight;
- break; // Not necessary, because of return-statement above
- case "Mars":
- return 0.377 * earthWeight;
- break; // Not necessary, because of return-statement above
- case "Jupiter":
- return 2.36 * earthWeight;
- break; // Not necessary, because of return-statement above
- case "Saturn":
- return 0.916 * earthWeight;break; // Not necessary, because of return-statement above
- default:
- return "Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.";
- }
- }
- console.log(calculateWeight(100, 'Jupiter')) // Should print 236
- 8.
- const truthyOrFalsy1111 = value => {
- if (value) {
- return true
- }
- return false
- }
- // Write your function here:
- function truthyOrFalsy(value){
- if(value === false || value === 0 || value === "" || value === '' || value === `` || value === null || value === undefined || value === NaN){
- return false;
- }
- else{
- return true;
- }
- }
- console.log(truthyOrFalsy(0)) // Should print false
- 9.
- // Write your function here:
- const numImaginaryFriends = (friends) => {
- return Math.round(0.33 * friends);
- }
- // Uncomment the line below when you're ready to try out your function
- console.log(numImaginaryFriends(18)) // Should print 6
- 10.
- const sillySentence = (adjective, verb, noun) => `I am so ${adjective} because I ${verb} coding! Time to write some more awesome ${noun}!`;
- console.log(sillySentence('excited', 'love', 'functions'); );
- 11.
- // Write your function here:
- function howOld(age, year){
- if(year >= 2020){
- return `You will be ${age + year - 2019} in the year ${year}.`
- }
- else if(2019 - age > year){
- return `The year ${year} was ${2019 - age - year} years before you were born.`;
- }
- else if(2019 - age < year && year <= 2019){
- return `You were ${year - 2019 + age} in the year ${year}.`;
- }
- }
- console.log("This code was written in 2019 and I am born at 1999, so suppose that current year is 2019 and I was 20 years old.");
- console.log();
- console.log("1. " + howOld(20, 2039));
- console.log("2. " + howOld(20, 2009));
- console.log("3. " + howOld(20, 1909));
- 12.
- const whatRelation = percentSharedDNA => {
- if (percentSharedDNA === 100) {
- return 'You are likely identical twins.'
- }
- else if (percentSharedDNA <= 99 && percentSharedDNA > 34) {
- return 'You are likely parent and child or full siblings.'
- }
- else if (percentSharedDNA <= 34 && percentSharedDNA > 13) {
- return 'You are likely grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings.'
- }
- else if (percentSharedDNA > 5 && percentSharedDNA <=13) {
- return 'You are likely 1st cousins.'
- }
- else if (percentSharedDNA > 2 && percentSharedDNA <= 5) {
- return 'You are likely 2nd cousins.'
- }
- else if (percentSharedDNA > 0 && percentSharedDNA <= 2) {
- return 'You are likely 3rd cousins'
- }
- return 'You are likely not related.'
- }
- console.log(whatRelation(34))
- // Should print 'You are likely grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings.'
- console.log(whatRelation(3))
- // Should print 'You are likely 2nd cousins.'
- 13.
- // Write your function here:
- const tipCalculator = (quality, total) => {
- switch(quality){
- case "bad":
- return 0.05 * total;
- break;
- case "ok":
- return 0.15 * total;
- break;
- case "good":
- return 0.2 * total;
- break;
- case "excellent":
- return 0.3 * total;
- break;
- default:
- return 0.18 * total;
- break;
- }
- }
- console.log(tipCalculator('good', 100)) //should return 20
- 14.
- // Write your function here:
- function toEmoticon(emotion){
- switch(emotion){
- case "shrug":
- return '|_{"}_|';
- break;
- case "smiley face":
- return ':)';
- break;
- case "frowny face":
- return ':(';
- break;
- case "winky face":
- return ';)';
- break;
- case "heart":
- return '<3';
- break;
- default:
- return "|_(* ~ *)_|";
- break;
- }
- }
- // console.log(toEmoticon("whatever"))
- // Should print '|_(* ~ *)_|'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement