0% found this document useful (0 votes)
40 views2 pages

Function Let Const Let If Let Const

The document contains code for two functions: stringAnagram and longestSubarray. The stringAnagram function takes a dictionary and query as inputs, checks if each element in the query is an anagram of a word in the dictionary, and returns an array indicating the number of anagrams found for each query element. The longestSubarray function takes an array as input, finds the longest subarray where each element is the same or adjacent to the previous element, and returns the length of the longest subarray.

Uploaded by

Ming Hsieh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

Function Let Const Let If Let Const

The document contains code for two functions: stringAnagram and longestSubarray. The stringAnagram function takes a dictionary and query as inputs, checks if each element in the query is an anagram of a word in the dictionary, and returns an array indicating the number of anagrams found for each query element. The longestSubarray function takes an array as input, finds the longest subarray where each element is the same or adjacent to the previous element, and returns the length of the longest subarray.

Uploaded by

Ming Hsieh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

function stringAnagram(dictionary, query) {

    let resultArr = [];
    [Link](el => {
        const wordToCheck = [Link]('');
        let anagram = 0;
        [Link](word => {
            if ([Link] === [Link]){
                let coincidence = 0;
                const wordArr = [Link]('').sort();
                [Link]();
                [Link]((value, i) => {
                    value === wordArr[i] ? coincidence++ : null;
                })
                coincidence === [Link] ? anagram++ : null
            }
        })
        [Link](anagram);
    })
    return resultArr;
}

function longestSubarray(arr) {
    const withoutRep = new Set();
    [Link](el => {
        [Link](el);
    })
    let coin;
    let max;
    [Link](el => {
        max = 0;
        coin = 0;
        for (const number of arr) {
            if (el === number || el + 1 === number || el - 1 === number){
                coin++;
            }
            else {
                break;
            }
        }
        coin > max ? max = coin : null;
    })
    return max;
}

You might also like