Home > Software engineering >  Solving homework "minor and major numbers"
Solving homework "minor and major numbers"

Time:02-05

the function called 'major and minor' receives as argument an array of numbers called 'numbers' and must return an array containing the smallest number in the array 'numbers' at position zero and the largest number in the array

number in the position 1.

Example:

minorMajor([4, 6, 1, 7, 15]) should return [1, 15]

given that 1 is the smallest number (minor) inside the array [4, 6, 1, 7, 15]

and 15 is the biggest number (major) inside the array [4, 6, 1, 7, 15]

function menorMayor(num){
[3,7,4,2,8]
for(let i=0; i<=num.legth; i  )
index=num  
}
if (0>[i]) return ('minor');
else if (0<[i]) return ('major');

And how is an exam, the terminal(console), say´s failure and "should return the smallest numbers, and bigger numbers

CodePudding user response:

here you go

function minMax(arr) {
  let mini = arr[0];
  let max = arr[0];
  let result = [];
  arr.forEach(num => {
    if (num > max) {
      max = num
    }
    else if (num < mini) {
      mini = num
    }
  });
  result.push(mini);
  result.push(max)
  return result;
}

console.log(minMax([2, 5, 32, 12, 45]))

CodePudding user response:

There is two function that exist in Javascript; Math.min() and Math.max()
You can use them in this code like this:

function menorMayor(num){
    array = [3,7,4,2,8]
    if (!array.includes(num)) return console.log("That number is not in array list.")
    if (Math.min(...array) === num) return console.log("That number is minor number of array.")
    if (Math.max(...array) === num) return console.log("That number is major number of array.")
    return console.log("This number is neither the major nor the minor number.")
}

menorMayor(num)

You can visit this Web Site for understand those 2 functions.

  • Related