JavaScript Projects for Beginners
JavaScript Projects for Beginners
A JavaScript program can find the years where the 1st of January is a Sunday by iterating over each year in the range and creating a date object for January 1st of each year. Using `date.getDay()`, check if the result is 0 (Sunday). If true, append the year to the output. This can be done with a simple loop from 2014 to 2050 in the `findSundayYears` function .
To rotate a string by moving its last character to the beginning periodically in JavaScript, you can use the `setInterval` function to perform the operation at regular intervals. Start by splitting the string into an array of characters. Use `arr.pop()` to remove the last character and `arr.unshift(lastChar)` to insert it at the beginning. Use `document.getElementById('output').innerText = arr.join('');` to display the rotated string. This approach ensures continuous string rotation every second as implemented in the given `rotateString` function .
The guessing game can be implemented by generating a random number between 1 and 10 using `Math.floor(Math.random() * 10) + 1`. The user is prompted to input a guessed number. If the guessed number matches the generated random number, an alert displays 'Good Work'; otherwise, it shows 'Not matched' with the correct number. This logic is handled in the `guessingGame` function .
Heron's formula calculates the area of a triangle when the lengths of all sides are known. The formula states that if a, b, and c are the lengths of the sides, and s is the semi-perimeter `(a+b+c)/2`, then the area is `√(s(s-a)(s-b)(s-c))`. The JavaScript implementation for sides 5, 6, and 7 calculates s as `(5 + 6 + 7) / 2 = 9` and then evaluates the area using the formula, resulting in the output from `displayTriangleArea()` .
The program checks if the current date is after December 25th of the current year. If so, it sets Christmas to December 25th of the next year. It then calculates the difference in milliseconds between now and Christmas, converts this to days using `24*60*60*1000` milliseconds per day, and rounds the result to get the days left. This is achieved using `daysUntilChristmas()` function .
The method used in JavaScript to print the contents of the current window is `window.print()`. This can be attached to a button click event to initiate the printing operation, a common feature in web applications for generating hard copies of documents or web page views .
To rotate the string 'resource', set an interval to remove the last character using `arr.pop()` and prepend it to the start using `arr.unshift(lastChar)`. The interval controls the periodic rotation, moving one character per second to the beginning, which simulates flowing rotation displayed in real-time .
The program obtains two numbers via user input with `prompt()`, parses them as floats, and then calculates multiplication using `num1 * num2` and division using `num1 / num2`. Results are displayed by updating a webpage element. This basic arithmetic operation is efficient for demonstrating user input handling and real-time calculation display within web pages .
The algorithm to determine if a year is a leap year checks whether the year is divisible by 4 but not by 100, or divisible by 400. This can be expressed in JavaScript as `let isLeap = ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0));` This checks the standard rules for leap years in the Gregorian calendar .
In determining if a given input year is a leap year, the program prompts the user for a year, checks the leap year conditions with `let isLeap = ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0));` If true, it displays the result in a webpage element, indicating if the input year is a leap year .