10 Days of JavaScript Hackerrank Solution10 days of JavaScript is a set of tutorials and challenges on the hackerrank website. Hackerrank is known for its challenging programming questions, which are crucial for a programmer to ace coding interviews and are foundational pavements in the learning journey of a student. 10 days of JavaScript is structured in such a way that it covers every aspect of JavaScript, from basic to advanced. Going through these challenges ensures that one has a good grasp of the foundational concepts of JavaScript. Solving these problems can be challenging sometimes, and taking suggestions and seeing solutions can help in such scenarios. This article aims to help you with this purpose. In it, we will discuss the solutions to problems from each day of the 10 days of JavaScript from hackerrank. Day 0: Hello World!Hello World of the 10 Days of JavaScript covers the basics of the language and introduces the learner to the very first concepts of a programming language. It covers printing outputs, identifiers, comments, basic syntax, and literals in the JavaScript programming language. Problem: A greeting function with the parameter variable parameterVariable is provided by the editor. To finish the challenge, display Hello, World! And the contents of the parameterVariable using console.log(). Solution: The editor provides the greeting function. It prints "Hello, World!" already. It has been added to the missing line: console.log(parameterVariable). This line prints the value of the parameterVariable argument that is passed to the function. Day 0: Data TypesThe next chapter unravels another very important concept of JavaScript: data types. Data Types of the 10 Days of JavaScript covers all the data types, type of operators, dynamic typing, naming of the variables, declaration, and initialization, etc. Problem: The editor declares the variables firstInteger, firstDecimal, and firstString. It would help if you used the + operator to concatenate firstString and secondString, turn secondDecimal to a floating-point number and add it to firstDecimal, and convert secondInteger to an integer and add it to firstInteger. Make sure firstString is printed first when you print the outcome on a new line. Solution: The logic for operations such as concatenating first and second strings, summing first and second integers, and summing first and second decimals is included in the perform Operation function. It parses the secondInteger string into a floating-point number, transforms it to a number, and outputs the outcome. The concatenation operator, +, is used in the function. Day 1: Arithmetic OperatorsArithmetic Operators of the 10 Days of JavaScript covers a crucial topic of JavaScript: operators. This section classifies operators as unary, binary, and ternary and covers arithmetic operators. Problem: In the editor below, do the following tasks:
Solution: The getArea and getPerimeter functions calculate the area and perimeter, respectively, using the formulas length * width and 2 * (length + width) and return the calculated values. Day 1: FunctionsThis chapter covers the basics of advanced functions in Javascript and introduces a very important concept of programming: recursion. Recursion occurs when a function call takes place within the function definition. Problem: Implement a function that takes n as a parameter and returns its factorial, i.e., n! Solution: The function factorial takes n as a parameter and returns 1 if n is 0 or 1. Otherwise, the function returns the product of n and factorial(n-1). Thus, the concept of recursion is used here to calculate the factorial of a given number. There can be another approach to solve this problem, i.e., using loops. Day 2: let and constThis chapter of the series covers let, var, and const in Javascript. These are useful in different scenarios, and this chapter discusses their use and importance. Solving the challenge below ensures a good grasp of this concept. Problem: Declare a constant variable, PI, and give it the value Math.PI. Next, read a value from stdin that represents a circle's radius, r. Use PI and r to get the area and perimeter of a circle with radius r. Console.log the parameter and area. Solution: This code declares the constant variable PI, and its value is Math.PI. The circle's radius is then read from stdin. Using the supplied formulas, the circle's area and perimeter are computed before printing the results using console.log(). Day 3: Conditional Statements: If-ElseThe very first chapter of conditional statements covers if-else statements, which are useful in many scenarios. The problem below presents a practice challenge. Problem: Finish the editor's getGrade(score) function. Its only argument is an integer that represents Julia's exam point total. The letter that corresponds to her grade must be returned. Solution: The code defines a function called getGrade that takes an integer score and returns its letter grade. It uses a series of if-else statements to determine the appropriate grade based on the following criteria: if the score is greater than or equal to 90, "A", "B", "C", "D", or "F", and if the score is greater than or equal to 60, "D". Otherwise, the grade is "F". Day 3: Conditional Statements: SwitchThis chapter unravels another important conditional statement, i.e., the switch statement. This allows one to check for various cases without using the hectic if-else-if ladder. Problem: Complete the getLetter(s) function in the editor. It has one parameter: a string consisting of lowercase English alphabetic letters (i.e., a through z). It must return A, B, C, or D depending on the following criteria: Solution: This code creates the method getLetter, which determines the category of the first letter in a string s by using a switch statement and a string s as input. The function uses the guidelines in the challenge to determine which letter ('A, B, C, or D') corresponds to it. Day 3: LoopsThis chapter explores a variety of JavaScript looping techniques and offers a thorough analysis of key control flow methods. It covers for, while, do-while, for-in, and for-of loops. Problem: First, print each vowel on a new line. The English vowels are a, e, i, o, and u, and each vowel must be printed in the same order as it appeared. Second, print each consonant (i.e., non-vowel) on a new line in the same order as it appeared. Solution: This code defines the vowelsAndConsonants function, which iterates through each letter in a string as input. It determines if the letter is a vowel and, if so, prints it on a new line. If it is a consonant, the letter is added to the consonant string, and the whole list of consonants is written on separate lines after the vowels. Day 4: ArraysThis chapter provides a thorough overview of the basic operations on arrays in JavaScript, enabling you to utilize the capabilities of this adaptable data structure fully. It starts with array generation and moves through important subjects like indexing and accessing array objects. Problem: This challenge teaches about arrays and their use in Python. The function getSecondLargest takes an array of integers as input and returns the second largest number in the array. Solution: This implementation removes duplicates from the array, sorts it in descending order, and then returns the second element, which is the second largest number. Day 4: Try, Catch, and Finally"String Basics" and "Error Handling," two essential components of JavaScript programming, are clarified in this chapter. Problem: Split, reverse, and join techniques may be used to reverse a string using the reverseString function, which takes a string input, s. If an exception is thrown, a new line is written with the message. The inverted string is returned if no exception was thrown, and the original string is returned if one was. Solution: This function attempts to reverse the input string using the split, reverse, and join methods. Day 4: ThrowThis chapter covers Javascript errors: try, catch, finally, and throw. Thus, it completes Javascript error handling. Problem: The argument for the isPositive function is an integer called a. It returns YES if an is positive and throws an error otherwise. It throws a Negative Error if an is negative and returns a Zero Error if it is 0. Solution: This function determines whether the integer entered is positive. The string "YES" is returned if an is positive. It throws an error with the message "Zero Error" if an is 0. It throws an error with the message "Negative Error" if an is negative. Day 5: Create a Rectangle ObjectThis chapter explores Javascript objects. It starts with a basic introduction and then proceeds to create objects. Problem: The function Rectangle provides an object that represents a rectangle and accepts two arguments, a and b. The rectangle object should have the factors that follow: area equal to the product of length and width, perimeter equal to the sum of length and width, and length equal to a. Solution: The above given code solves the proposed problem perfectly. Day 5: Count ObjectsThis chapter covers for, for in, forEach loops to count objects. Problem: The editor function requires an array of objects with two integer properties, x and y. Its goal is to return the count of objects o in the array satisfying o.x==o.y. Solution: Day 5: ClassesThis chapter covers the basics of object-oriented programming in JavaScript. Problem: Make a Polygon class and test the perimeter() function and constructor in the editor. The constructor accepts an array of integer values for side lengths, and the perimeter method returns the polygon's perimeter. Solution: This code presents the complete implementation of the solution for the given problem. Day 6: InheritanceThis chapter of the 10 days of JavaScript set covers the basics of inheritance in JavaScript. Problem: The editor offers a Rectangle class implementation; to utilize it, you must add an area method to the prototype and make a square class that is a subclass of Rectangle, has a constructor, and can use the area method of the Rectangle class. Solution: The Square class is constructed as a subclass of Rectangle using the extends keyword, whereas the Rectangle class contains an area method for computing the rectangle area. Super(side, side) is how the Square class builds a constructor with a side length. Day 6: Template LiteralsProblem: The function sides, which accept literals, an array of strings, and expressions, and a rest parameter holding values supplied via string interpolation, are needed to solve the problem. Using the formulas s₁ and s₂ = P ± √(P² - 16A)) / 4, the function should be able to determine the side lengths of a rectangle depending on its area and perimeter. Solution: Day 6: Arrow FunctionsThis chapter explores the introduction and implementation of arrow functions. Problem: The editor's function accepts an array, nums, and multiplies even and odd items by two and three, each time iterating over the array before returning the updated array. Solution: Day 7: Bitwise OperatorThis chapter covers bitwise operators in JavaScript. It starts with number system conversions and then explores each bitwise operator with example usage. Problem: The task requires you to determine the greatest bitwise AND value that is less than a specific integer, k, for any two numbers, a and b, in a sequence S. Solution: Day 7: JavaScript DatesThis chapter explores the dates and times in JavaScript and the method to fetch the dates using get. Problem: The function returns the day name of a date string in the format MM/DD/YYYY, which can be Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, or Saturday, as an example, for example, 12/07/2016. Solution: Day 8: Regular Expressions IThis chapter covers the basics of regular expressions in JavaScript. Problem: Returning a RegExp object, re, that matches any string s that starts and ends with the same vowel will complete the method in the editor below. Remember that there are five vowels in English: a, e, i, o, and u. Solution: Day 8: Regular Expressions IICovers intermediate level concepts of regular expressions in javascript. Problem: The editor's method produces a RegExp object called re, which matches any text that satisfies two requirements: the string must begin with one of the prefixes Mr., Mrs., Ms., Dr., or Er, and the remaining characters must be uppercase, lowercase, or capital letters in the English alphabet. Solution: Day 8: Regular Expressions IIIThis chapter of day 7 covers the advanced-level concepts of regular expressions in JavaScript. Problem: In the editor below, finish the function by giving back a RegExp object, re, that corresponds to each number in some string s. Solution: Day 9: Create a ButtonThis chapter explores designing buttons in html and adding functionality to them using JavaScript. Problem: Make a clickable button with the following properties: id: btn, style properties: 96px width, 48px height, font size: 24px, initial text label: 0, increased by 1 with each click. The text label is an innerHTML attribute of the button. The height and breadth of the button are given. Solution: Day 9: Buttons ContainerThis chapter explores creating button containers and scaling button layouts using JavaScript. Problem: The task involves creating nine buttons in a div, forming a 3 * 3 grid with distinct labels from 1 to 9, and ensuring that the outer buttons rotate clockwise when the middle button is clicked. The code must meet specific criteria in the editor. Solution: Day 10: Binary CalculatorThis chapter covers the making of a calculator using multiple events. Problem: Implement a simple calculator that performs the following operations on binary numbers: addition, subtraction, multiplication, and division. Note that division operation must be integer division only; for example,1001/100 = 10. Solution: Next TopicBinary Numbers in JavaScript |