在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):tejas-2232/Algorithmic_javascript开源软件地址(OpenSource Url):https://github.com/tejas-2232/Algorithmic_javascript开源编程语言(OpenSource Language):JavaScript 95.7%开源软件介绍(OpenSource Introduction):Algorithmic_javascript
Contribute to open source and make the world A Better PlaceFeel free to give a pull request. It should not be spam or invalid. It will be accepted and will be merged if it's valid PRPS:You will also WIN cool T-shirt from Digital Ocean If you do FOUR successful Pull Requests with.
Sign-in on their website before contributing to repositoryEvent DetailsHacktoberfest® is open to everyone in our global community. Whether you’re a developer, student learning to code, event host, or company of any size, you can help drive growth of open source and make positive contributions to an ever-growing community. All backgrounds and skill levels are encouraged to complete the challenge.
Rules
Quality StandardsIn line with Hacktoberfest values (Quantity is fun, quality is key) , here you are provided examples of pull requests that we consider to be low-quality contributions (which we discourage).
Last but not least, one pull request to fix a typo is fine, but 5 pull requests to remove a stray whitespace is not.
If you want to contribute then follow theses steps
Star the repository If you enjoyed contributing to open source projects.Algorithms practiced using JS List of problems
Explanation1. String reversing The string reversal algorithm is perhaps the most common JavaScript code challenge on the internet. In this article, we explore various string reversal techniques as a good number of string manipulation algorithms are dependent on ones ability to reverse a string. The challenge: Given a string of text, write an algorithm that returns the text received in a reversed format. E.g reverseString('algorithm')
// should return "mhtirogla" Algorithmic Thinking: The process here is quite simple and straight forward. Our function will receive only one parameter i.e the string to be reversed. Next, we would have to manipulate the characters in this string logically in order to return the string in reverse. Code Implementation: We will now explore ways to solve this challenge below. They are:
1.Chaining in-built methods:
In the code snippet below we use these methods to create a one-line solution by chaining them in succession to split the received text into an array of characters, reverse the order of array’s elements and join the elements of the reversed array to produce the reversed text. function reverseString(text){
return text.split("").reverse().join("")
} 2.For-Loop Way: For loops are used to execute a piece of code as long as a condition is met. In the solution below, we use a decrementing for-loop to run through the string received and append each character to another variable in reverse order. See how this is done below function reverseString(text){
let result;
for(let i=text.length-1;i>=0,i--){
result+=text[i];
}
return result;
} 2. Vowel counter Here we will be working with strings and arrays. The main challenge is given in The challenge section below.Let's find it The challenge: You are given a string of text containing zero or more vowels in it,count the number of vowels that can be found in given text. For example:
Algorithmic Thinking: After reading the problem statement, __ given text of string__ must be in your mind. Let's go further to explore
Let's Breakdown the approach
Code Implementation: We are going to use Two approaches for solving this problem:
1.Using Iterative approach: const vowel = ["a", "e", "i", "o", "u"];
function vowelcnt(text) {
let counter = 0;
//Loop through text to test if each character is a vowel
for (let letter of text.toLowerCase()) {
if (vowel.includes(letter)) {
counter++;
}
}
// Return number of vowels
return counter;
}
console.log(vowelcnt("i love HacktoberFest")) Breaking-down the steps:
3. Finding the Most Recurring Characterg In this challenge, we will be dealing with strings, arrays and objects. We will learn to manipulate these data types using various methods that'd bring us to a working solution in the end. The challenge: Given a string of text, find and return the most recurring character. e.g maxRecurringChar('aabacada') // will return 'a' Algorithmic Thinking: From the challenge statement, we can infer that our function has only one parameter; the string of text. Code Implementation: We need to keep track of every character in the string as well as the number of times it exists. The main concept we need here is character mapping. Our aim is to map characters to the number of times they exist. for example: In string "success"
To implement this, an objet can be used.We loop through string received & add each character to a character map object as a key & the number of times it exists as a value let charmap = {
s:3,
u:1,
c:2,
e:1
} Let's implement it /*
maxCharValue is used to store the maximum value yet encountered at the point of every iteration with the for---in loop.
maxChar is used to store the character with the highest value on every iteration.
*/
function maxRecurringChar(text) {
let charMap = {}
let maxCharValue = 0
let maxChar = ''
for (let char of text) {
if (charMap.hasOwnProperty(char)) {
charMap[char]++
} else {
charMap[char] = 1
}
}
for (let char in charMap) {
if (charMap[char] > maxCharValue) {
maxCharValue = charMap[char]
maxChar = char
}
}
return maxChar
}
console.log(maxRecurringChar('success'))
// will return 's' because it is occuring 3 times 4. Sentence Capitalization Often during presentation situation arises where we need to manipulate the letter casing of strings within our application. Javascript offers two popular methods designed for this purpose:
The challenge: Given a sentence containing two or more words, return the equivalent of the sentence when capitalized. E.g capitalSentence("a cat and a dog") // would return "A Cat And A Dog" Algorithmic Thinking: At a glance this may seem like a very simple challenge, but it actually isn’t when considered carefully. Working through the challenge, it seems that we need to write a function that'd receive the sentence to be converted as an argument Next,we go through every word in sentence and capitilize every first letter of word. This brings concept of LOOP to mind Code Implementation:
5. Palindromes What is a Palindrome: A palindrome is a word, number, or other sequence of characters which reads the same backward as forward, such as "madam" or "racecar". The challenge: Given a string of text, return true or false indicating whether or not the text is a palindrome. e.g palindromeChecker('racecar') // will return true Algorithmic Thinking: According to challenge,"Given a string of text" implies that our funciton must have string-typed parameter which we call "text" code Implementation: In this challenge, we'd consider two, yet three ways to implement this as highlighted below:
Let's unveil the "mysteries":
Let's review it:
6. Pig Latin Translator For specific information on Pig Latin, view this article. The challenge: Convert a string of text into Pig Latin. Algorithmic Thinking: We will consider two(2) ways to implement this function in JavaScript. They are: An imperative approach A declarative approach Before going ahead to implement both solutions, it’d be helpful to gain a better understanding of the two terms used above. Imperative vs Declarative Very often, we find these terms thrown around like they are very simple concepts everyone should know. However, the difference is usually not much obvious to most. Simply put, an imperative style of programming is one which specifies how things get done. Although this might sound like what you do each time you write code, there's a difference to it. Imagine you were to add an array of numbers and return the sum, there are different ways you could approach the problem. One way could be writing a forloop that'd go over each element in the array and cumulatively add every element to an accumulator until the final sum is reached. That is imperative. You are specifying how things get done. On the other hand, a declarative approach would abstract this process, allowing you to specify what should be done rather than how. Thus, you may use the .reduce() method on the array to reduce every element to a final value by returning the sum within the call back. code Implementation:
We start by converting the received string str to lowercase. This is to prevent any casing related errors during comparison(“a” does not equal “A”). // Convert string to lowercase
str = str.toLowerCase() Next, we initialize two variables: // Initialize array of vowels
const vowels = ["a", "e", "i", "o", "u"];
// Initialize vowel index to 0
let vowelIndex = 0; vowels - containing the five English vowels vowelIndex - for storing the index at which the first vowel in the word is found. It is initialized to 0. We use an if…else statement to check if the first letter of the word can be found within our vowels array by calling the .includes() method on the array while passing it the first letter of the string str[0]. If it is found, this returns true, which implies that the first letter is a vowel. Hence, we simply add " if (vowels.includes(str[0])) {
// If first letter is a vowel
return str + "way";
} else {
...
} If the statement evaluates to false, it signifies that the starting character is a consonant. Hence, we use a for…of loop to iterate through the string to identify the position of the first vowel. When we locate the first vowel, we use the .indexOf() method to retrieve it’s position in the string and store it into the variable vowelIndex. After this step we terminate the loop using the break statement. // If the first letter isn't a vowel i.e is a consonant
for (let char of str) {
// Loop through until the first vowel is found
if (vowels.includes(char)) {
// Store the index at which the first vowel exists
vowelIndex = str.indexOf(char);
break;
}
} At the last line, we use the .slice() method to manipulate the string to generate the Pig Latin equivalent. return str.slice(vowelIndex) + str.slice(0, vowelIndex) + "ay";
In this approach, we implement a very concise solution to this challenge by combining the .replace() method and regular expressions to transform the received string into its Pig Latin equivalent. Our solution comprises mainly of two parts as analyzed below: str.replace(/^([aeiouy])(._)/, '$1$2way') The first .replace statement specifies a replacement to be carried out if the word begins with a vowel. This is specified in the first bracket within the_ .replace() method call i.e ([aeiou]). The second bracket (.)* refers to every other character after the vowel. Thus, the expression specifies a pattern for words beginning with a vowel and followed by anything else. When this case is matched, a new string in the format of '
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论