# Check if an array is growing by the same margin in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in an array of numbers. Our function should return true if the difference between all adjacent elements is the same positive number, false otherwise.

## Example

The code for this will be −

const arr = [4, 7, 10, 13, 16, 19, 22];
const growingMarginally = arr => {
if(arr.length <= 1){
return true;
};
const diff = arr - arr;
if(diff < 0){
return false;
}
for(let i = 0; i < arr.length - 1; i++){
if (arr[i+1] - arr[i] !== diff){
return false;
}
}
return true;
};
console.log(growingMarginally(arr));

## Output

And the output in the console will be −

true
{"ft_lang_label":"__label__en","ft_lang_prob":0.6363459,"math_prob":0.93720865,"size":597,"snap":"2021-43-2021-49","text_gpt3_token_len":169,"char_repetition_ratio":0.11973018,"word_repetition_ratio":0.0,"special_character_ratio":0.3400335,"punctuation_ratio":0.1904762,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.970209,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-16T10:03:26Z\",\"WARC-Record-ID\":\"<urn:uuid:481681ed-ebd9-4248-8d20-b152372072c3>\",\"Content-Length\":\"31121\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5eeeff16-3f35-4134-b2d6-3de669b51568>\",\"WARC-Concurrent-To\":\"<urn:uuid:27bf0939-543f-498d-97b6-4d494857ea4f>\",\"WARC-IP-Address\":\"72.21.91.42\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/check-if-an-array-is-growing-by-the-same-margin-in-javascript\",\"WARC-Payload-Digest\":\"sha1:SEXEI5BPA3GH5AZLNYBGQO2AUEXPGQPT\",\"WARC-Block-Digest\":\"sha1:TAO4JO7RU4J2DAMD7YUQCTTGKBUZFSA5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323584554.98_warc_CC-MAIN-20211016074500-20211016104500-00649.warc.gz\"}"}