w3resource

JavaScript: Find the position of a rightmost round number in an array of integers. Returns 0 if there are no round number

JavaScript Basic: Exercise-139 with Solution

Write a JavaScript program to find the position of the rightmost round number in an array of integers. If there are no round numbers, the function returns 0.

Note: A round number is informally considered to be an integer that ends with one or more zeros.

Visual Presentation:

JavaScript: Find the position of a rightmost round number in an array of integers. Returns 0 if there are no round number.

Sample Solution:

JavaScript Code:

/**
 * Function to find the index of the rightmost round number (divisible by 10) in the array
 * @param {number[]} input_arr - The input array of numbers
 * @returns {number} - The index of the rightmost round number (if found)
 */
function find_rightmost_round_number(input_arr) {
  var result = 0; // Variable to store the index of the rightmost round number (default: 0)
  for (var i = 0; i < input_arr.length; i++) {
    if (input_arr[i] % 10 === 0) { // Checking if the number is divisible by 10
      result = i; // Updating 'result' with the current index if the number is divisible by 10
    }
  }
  return result; // Returning the index of the rightmost round number
}

console.log(find_rightmost_round_number([1, 22, 30, 54, 56]));  
console.log(find_rightmost_round_number([1, 22, 32, 54, 56]));  
console.log(find_rightmost_round_number([1, 22, 32, 54, 50]));   

Output:

2
0
4

Live Demo:

See the Pen javascript-basic-exercise-139 by w3resource (@w3resource) on CodePen.

Flowchart:

Flowchart: JavaScript - Find the position of a rightmost round number in an array of integers. Returns 0 if there are no round number

ES6 Version:

/**
 * Function to find the index of the rightmost round number (divisible by 10) in the array
 * @param {number[]} input_arr - The input array of numbers
 * @returns {number} - The index of the rightmost round number (if found)
 */
const find_rightmost_round_number = (input_arr) => {
  let result = 0; // Variable to store the index of the rightmost round number (default: 0)
  
  for (let i = 0; i < input_arr.length; i++) {
    if (input_arr[i] % 10 === 0) { // Checking if the number is divisible by 10
      result = i; // Updating 'result' with the current index if the number is divisible by 10
    }
  }
  
  return result; // Returning the index of the rightmost round number
}

console.log(find_rightmost_round_number([1, 22, 30, 54, 56]));  
console.log(find_rightmost_round_number([1, 22, 32, 54, 56])); 
console.log(find_rightmost_round_number([1, 22, 32, 54, 50]));

Improve this sample solution and post your code through Disqus.

Previous: JavaScript program to reverse the bits of a given 16 bits unsigned short integer.
Next: JavaScript program to check if all the digits in a given number are the same or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-139.php