https://leetcode.com/problems/return-length-of-arguments-passed/editorial/ We need to count the number of arguments passed to function argumentsLength . Here the arguments are passsed in the form of rest parameters. why do we need to pass it like that? the rest parameter is a feature that allows a function to accept an indefinite number of arguments The rest parameter collects all the remaining arguments passed to a function into an array even if the number of arguments is not known in advance. If no additional arguments are passed, the rest parameter will be an empty array Syntax: function functionName ( ... args ) { // Function body } conventionally named args or rest to indicate its purpose. function sum ( ... args ) { let total = 0 ; for ( let number of args ) { total += number ; } return total ; } console . log ( sum ( 1 , 2 , 3 , 4 ) ) ; // Output: 10 console . log ( sum ( 5 , 10 , 15 ) ) ; // Output...
https://leetcode.com/problems/palindrome-linked-list/description/ https://www.w3schools.com/jsref/jsref_slice_array.asp const arr=[1,2] ; console.log(arr.reverse().join()===arr.join()); ChatGPT The expression arr.reverse() reverses the order of elements in the arr array in place, meaning it modifies the original array. The arr.join() method converts the elements of the array into a string, with each element separated by a comma by default. In the given code, arr.reverse().join() returns a string representation of the reversed array. However, the original arr array has also been reversed because the reverse() method modifies the array directly. Therefore, arr.reverse().join() === arr.join() will evaluate to true because both sides of the comparison refer to the same array that has been reversed and then converted to a string. const newarr =arr; return newarr.reverse().join()===arr.join() ; ChatGPT In the code you provided, newarr is assigned the reference to the arr ...
function find_max ( nums ) { let max_num = Number . NEGATIVE_INFINITY ; // smaller than all other numbers for ( let num of nums) { if (num > max_num) { max_num = num; // Update max_num with the current number if it's greater } } return max_num; } In this code, max_num starts as Number.NEGATIVE_INFINITY , which is smaller than any other number. The loop iterates through each number in the nums array and compares it with the current max_num . If the current number is greater, it updates max_num to the new maximum. After the loop finishes, the function returns the maximum number found.
Comments
Post a Comment