Hello, good evening! I’m in need of help solving javascript array exercise. I’m Beginner, I appreciate all help

Asked

Viewed 61 times

-2

Prepare a program that reads two matrices A and B of a six-element dimension. The matrix A must accept only the input of even values, while the matrix B must accept only input of odd values. The input of the matrices must be validated by the program. Construct the matrix C that is the result of joining the matrices A and B, so that the matrix C contains 12 elements. Display the elements of matrix C.

console.log('Exercício 06')
var matriz06A = [];
var matriz06B = [];
var matriz06C = [];
function fpares (num){
    n = num;
    if (n % 2 == 0){
        return (n);
    }
}

function fimpares (num){
    n = num;
    if (n % 2 == 1){
        return (n);
    }
}


function felementosA (){
    for (var i = 0; i < 6; i++){
        matriz06A.push(fpares(Math.floor(Math.random() * 25 +1)))
        matriz06A = matriz06A.filter(function(item){
            return item != null
        })
    }
return matriz06A;        
}

function felementosB (){
    for (var i = 0; i < 6; i++){
        matriz06B.push(fimpares(Math.floor(Math.random() * 25 +1)));
        matriz06B = matriz06B.filter(function(item){
            return item != null
        })
    }
return matriz06B;        
}

felementosA();
felementosB();
matriz06C = matriz06A.concat(matriz06B);
console.log('Matriz A: '+matriz06A); //este vetor deve possuir 6 elemento
console.log('Matriz B: '+matriz06B);
console.log('Matriz C: '+matriz06C);
console.log(`A matriz C possui ${matriz06C.length} elementos.`)
  • Hello, take a minute of your time to read about How not to ask questions, will help you improve the body of your questions and get answers easier.

  • Hello Leo, good morning! Thanks for the tip regarding good practices.

1 answer

-3


Why are you generating the matrix? It is not for the user to enter the matrices?

(function () {
  function getValue(title) {
    return window.prompt(title);
  }
  
  function getMatrix(name, length, breakPredicate) {
    var M = [];
    for (var i = 0; i < length; ++i) {
      var element = 0;
      do {
        element = getValue("Matriz " + name + ", elemento " + i + 
          " (entre número inteiro de 32 bits não-negativo):");
      } while (element === null 
               || !/^\d{1,10}$/.test(element)
               || element.trim() === ''
               || isNaN((element = +element))
               || element < 0 
               || !breakPredicate(element));
      M.push(element);
    }
    return M;
  }
  
  var A = getMatrix("A", 6, function(x) { return x % 2 === 0; });
  var B = getMatrix("B", 6, function(x) { return x % 2 !== 0; });
  var C = A.concat(B);
  
  alert("A matriz C é " + JSON.stringify(C));
})();

  • Hello, Marcelo, good morning!

  • Marcelo, I am generating random values for the matrix, in order to speed up the process of obtaining the values for the matrix. I appreciate the feedback, I’ll try to solve the exercise.

  • I managed to do the exercise. Very Grateful!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.