[アルゴリズム]分割された数値配列


  • mapで解いた
  • function solution(arr, divisor) {
        let answer = [];
        let a = arr.map((el)=>{
            if(el % divisor === 0) answer.push(el)
               
        })
        if(answer.length === 0){
            answer = [-1]
        }
        return answer.sort( (a,b) => {
            return a < b ? -1 : 1
        })
        }
  • function solution(arr, divisor){
        var answer = [];
        
        for(let i = 0; i < arr.length; i++){
            if(arr[i] % divisor === 0){
            answer.push( arr[i])
            }
        }
        return answer.length === 0
        ? [-1]
        : answer.sort( (a,b) => a - b )
    }
  • メソッド
  • を使用
    function solution(arr, divisor){
        const answer = arr.filter(num => {
            return num % divisor === 0
        })
        return answer.length === 0
        ? [-1]
        : answer.sort( (a,b) => a-b)
        
    }