Home > Back-end >  how to check number in percentage and print it in an array or object, if number is exits then it sho
how to check number in percentage and print it in an array or object, if number is exits then it sho

Time:03-25

in a array of [5,10,15,20,40]. we need to check the number with % and if we get 5 and 10 then we will take it in 10% and print it 1 so for 10 to 20 we consider as 20% and check the number if we get any number from there then we will increase the number like wise we need to calculate. for example [ 10, 4, 40, 50, 70] the max number is 100 so that's the 100th% and the result should be [1, 1, 0, 1, 1, 0, 1, 0, 0, 0] there is 1 element in 0-10% (4), 1 in 10-20% (10), none in 20-30%, and so forth. –

function calculating_marks(x) {
  for (let i = 0; i < x.length; i  ) {
    x[i] <= 10 ? console.log("1") : console.log("0")
    x[i] > 10 && x[i] <= 20 ? console.log("1") : console.log("0")
    x[i] > 20 && x[i] <= 30 ? console.log("1") : console.log("0")
    x[i] > 30 && x[i] <= 40 ? console.log("1") : console.log("0")
    x[i] > 40 && x[i] <= 50 ? console.log("1") : console.log("0")
    x[i] > 50 && x[i] <= 60 ? console.log("1") : console.log("0")
  }
}

marks = [5,10,20,30,35,50]
calculating_marks(marks)

CodePudding user response:

This might be a solution to your question, I hope it solves your problem:

function calculating_marks(marks) {
  const marksResult = Array(10).fill(0);
  for (const mark of marks) {
    let markConverted = Math.floor(mark/10) - 1;
    if(markConverted === -1){
      markConverted  ;
    }
    marksResult[markConverted]  ;
  }
    return marksResult;
}

let marks = [5,10,20,30,35,50]
let marksConverted = calculating_marks(marks)
console.log(marksConverted)

CodePudding user response:

Save in a array and plus if true

function calculating_marks(x) {
   // count for 10%,20%,.....100%
   var count = [0,0,0,0,0,0,0,0,0,0];
   for (let i = 0; i < x.length; i  ) {
    x[i] <= 10 ? count[0]   :
    x[i] > 10 && x[i] <= 20 ? count[1]   : 
    x[i] > 20 && x[i] <= 30 ? count[2]   : 
    x[i] > 30 && x[i] <= 40 ? count[3]   : 
    x[i] > 40 && x[i] <= 50 ? count[4]   : 
    x[i] > 50 && x[i] <= 60 ? count[5]   : false
   }
  console.log(count);
}

marks = [5,10,20,30,35,50]
calculating_marks(marks)

//Result (10) [2,1,1,1,1,0,0,0,0,0]

CodePudding user response:

Here is possible approach with inline comments:

// Your function with custom calculation
const weirdCalculator = arr => {
  // Find largest number
  const largest = arr.sort((a,b) => a-b).reverse()[0];
  
  // Find 10% of largest number
  const p10 = Math.round(largest / 10);
  
  // Create result array
  const res = [];
  
  // Loop 10 times
  for(let i = 1; i <= 10; i  ) {
    // With each new iteration add 10% and check
    // all numbers, passed to function, in loop
    // Calculate current range
    const currentRange = [(i - 1) * p10, i * p10];
    // Reset current iteration counter
    let counter = 0;
    // Loop numbers, passed to function
    // If number in current range - increase counter
    for(const num of arr) if(num > currentRange[0] && num <= currentRange[1]) counter  ;
    // Push counter to result array
    res.push(counter);
  }
  
  // Return result
  return res;
}

// Run function
const result = weirdCalculator([5,10,20,31,34,50]);

// Test
console.log(result);

  • Related