Examen de Programmation JavaScript TS
Examen de Programmation JavaScript TS
The jQuery method used to add a CSS class to an element is `addClass()`. You would use this method over vanilla JavaScript for its simplicity and cleaner syntax, especially when manipulating multiple elements or applying multiple class changes, as jQuery methods are chainable and handle browser inconsistencies effectively .
To display the number of students who have passed using jQuery, first filter the data based on the condition that defines 'passed', such as a score of 10 or higher being 'Admis'. Count these entries, and then display the result within a specific division using jQuery's text manipulation methods, such as `$('#passedCount').text(count)`, to reflect the calculated number of admitted students in the specified HTML element .
The correct keyword to generate a custom exception in JavaScript is `throw`. You can use it to throw any expression, such as an error object or a message of your choice, that can then be caught by a surrounding `try...catch` block to handle the error. For example, `throw new Error('Custom error message');` creates a new Error object with the specified message .
To calculate a student's average grade with weighted coefficients, first sum the products of each grade and its respective coefficient. Then, divide this sum by the total of the coefficients to find the weighted average. For example, if grades are [grade1, grade2] with coefficients [coef1, coef2], the algorithm is ((grade1 * coef1) + (grade2 * coef2)) / (coef1 + coef2). This ensures each grade's influence is proportionate to its assigned weight in the overall average calculation .
The method used to create an AJAX request in JavaScript is `fetch()`. This method returns a Promise that resolves to the Response object representing the response to the request. It is used for handling HTTP requests in a modern, promise-based manner, allowing for simpler and more readable asynchronous code when compared to the older `XMLHttpRequest` method. Despite its convenience, developers need to handle any potential network errors and utilize `Promise` methods such as `then()` and `catch()` to process the results .
The result of the regular expression /[0-9]{2}/ applied to the string '42' is `true`. This indicates that the pattern is looking for exactly two consecutive digits between 0 and 9, and it successfully finds those in the string '42', matching the two-digit pattern .
You can access the next sibling element of a selected element in the DOM using the `nextElementSibling` property. For example, `element.nextElementSibling` allows you to programmatically navigate to the next sibling that is an element node, skipping over any text or comment nodes .
In validating a student's registration number and phone number using JavaScript, the registration number must begin with 'Stg' followed by four digits, such as 'Stg2123'. This requires a regular expression to match this specific pattern. Meanwhile, the phone number must exactly consist of 10 digits, which can be validated using another regular expression to ensure there are neither more nor fewer digits. Both forms of validation are crucial to maintain data integrity and format consistency .
To select elements with a specific CSS class containing certain text using jQuery, you would use the selector `$('.className:contains("text")')`. This selects all elements with the given class name and also containing the specified text in their content. For example, `$('.example:contains("jQuery")')` selects elements with the class `example` that include the text 'jQuery' .
To programmatically count and display non-passing students using jQuery or JavaScript, start by filtering the data set for those who did not meet the 'passing' condition, such as having a score lower than 10. Once counted, output this total into an HTML division using a jQuery method like `.text()` or `.html()`, targeting the division by its ID or class, for instance, `$('#nonPassedCount').text(nonPassedCount)` .