JavaScript-coding-challenges

by Mr_Vasu

JavaScript

/* referance link: https://github.com/rradfar/javascript-coding-challenges */
let inputArray = [
    {
        "id": 1,
        "name": "Albert",
        "marks": {
            "sci": 80,
            "maths": 82,
            "eng": 90
        },
        "percentage": "first Class",
        "rank": "84.0"
    },
    {
        "id": 2,
        "name": "Binoy",
        "marks": {
            "sci": 40,
            "maths": 82,
            "eng": 70
        },
        "percentage": "second class",
        "rank": "64.0"
    },
    {
        "id": 3,
        "name": "Charles",
        "marks": {
            "sci": 98,
            "maths": 98,
            "eng": 100
        },
        "percentage": "distiction",
        "rank": "98.7"
    }
]

/* find the avarage and add the value to percentage and pass or fail */
function ArrayMethod2(array) {
    array.map((i) => {
        const total = Object.values(i['marks']).reduce((a,c) =>a + c , 0); 
        // console.log(total);
        let someOfAvarage = (total / Object.values(i['marks']).length).toFixed(1);
        let eachPercentage = '';
        if(someOfAvarage >= 85){
            eachPercentage = 'distiction';
        }else if( someOfAvarage < 85 && someOfAvarage > 70){
            eachPercentage = 'first Class';
        }else if(someOfAvarage <= 70 && someOfAvarage > 40 ){
            eachPercentage = 'second class';
        }else{
            eachPercentage = 'faild';
        }
        // console.log(eachPercentage);
        // console.log(someOfAvarage);
        i.percentage = eachPercentage;
        i.rank = someOfAvarage;
	    })
		console.log(array);
    return array;
    
}
ArrayMethod2(inputArray);


// 1. Multiples of 3 or 5
/* If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.

Note: If the number is a multiple of both 3 and 5,...