0% found this document useful (0 votes)
8 views3 pages

JJ

The document contains code for 4 functions: 1) findMaxValue() to find the maximum value in an array 2) countOccurrences() to count the occurrences of an element in an array 3) generateFibonacciSequence() to generate a Fibonacci sequence of a given length 4) isEven() to check if a number is even, and a function using it to square even numbers in an array and store in a new array

Uploaded by

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

JJ

The document contains code for 4 functions: 1) findMaxValue() to find the maximum value in an array 2) countOccurrences() to count the occurrences of an element in an array 3) generateFibonacciSequence() to generate a Fibonacci sequence of a given length 4) isEven() to check if a number is even, and a function using it to square even numbers in an array and store in a new array

Uploaded by

sarah ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

function findMaxValue(arr) {

if (arr.length === 0) {
return undefined;
} else {

var maxVal = arr[0];

// Iterate through the array starting from the second element


for (let i = 1; i < arr.length; i++) {
// Update the maximum value if a larger value is found
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}

return maxVal;
}
}

// Example usage
const myArray = [5, 8, 2, 10, 3];
const maxValue = findMaxValue(myArray);
console.log("Maximum value:", maxValue);

function countOccurrences(arr, element) {


var count = 0;

for (var i = 0; i < arr.length; i++) {


if (arr[i] === element) {
count++;
}
}

return count;
}

// Example usage
var myArray = [1, 2, 3, 2, 4, 2, 5];
var targetElement = 2;
var occurrenceCount = countOccurrences(myArray, targetElement);
console.log("Occurrences of", targetElement + ":", occurrenceCount);

///////////////////////////////////
Occurrences of 2: 3

function generateFibonacciSequence(length) {
if (length <= 0) {
return [];
} else if (length === 1) {
return [0];
} else if (length === 2) {
return [0, 1];
} else {
var sequence = [0, 1];

for (var i = 2; i < length; i++) {

var nextNumber = sequence[i - 1] + sequence[i - 2];

sequence.push(nextNumber);
}

return sequence;
}
}

var sequenceLength = 10;


var fibonacciSequence = generateFibonacciSequence(sequenceLength);
console.log("Fibonacci Sequence:", fibonacciSequence);

// Define the numbers array


const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Function to check if a number is even


function isEven(number) {
return number % 2 === 0;
}

// Initialize the evenNumbers array


const evenNumbers = [];

// Iterate through the numbers array


for (let i = 0; i < numbers.length; i++) {
const number = numbers[i];

// Check if the number is even


if (isEven(number)) {
// Create the squareObject containing the even number and its square
const squareObject = {
number: number,
square: number * number
};

// Add the squareObject to the evenNumbers array


evenNumbers.push(squareObject);
}
}

// Print the evenNumbers array to the console


console.log("Even Numbers and Their Squares:");
console.log(evenNumbers);

You might also like