Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a JavaScript function findMaxSequence(arr) that finds the maximal increasing sequence
- in an array of numbers and returns the result as an array. If there is no increasing sequence
- the function returns 'no'. Write JS program maxSequenceFinder.js that invokes your function
- with the sample input data below and prints the output at the console. */
- "use strict";
- function findMaxSequence(arr) {
- if (arr.length <= 1) {
- return 'no';
- } else {
- var resultArr = [];
- var tempArr = [arr[0]];
- var i;
- for (i = 1; i < arr.length; i += 1) {
- if (arr[i] > arr[i - 1]) {
- tempArr.push(arr[i]);
- } else {
- if (tempArr.length > resultArr.length) {
- resultArr = tempArr.slice(0);
- }
- tempArr = [arr[i]];
- }
- }
- if (tempArr.length > resultArr.length) {
- resultArr = tempArr.slice(0);
- }
- if (resultArr.length > 1) return resultArr;
- else return 'no';
- }
- }
- console.log(findMaxSequence([3, 2, 3, 4, 2, 2, 4]));
- console.log(findMaxSequence([3, 5, 4, 6, 1, 2, 3, 6, 10, 32]));
- console.log(findMaxSequence([3, 2, 1]));
- console.log(findMaxSequence([4, 4, 5, 6, 10, 32, 1, 2, 3, 4, 5, 6]));
- console.log(findMaxSequence([5]));
- console.log(findMaxSequence([-9.3, -6.5, -1.5, 7.8, 3.2, 3.5, 4.3, 5.4, 6.2, 2.1, 4.8]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement