Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
How to print the 2nd biggest number without using function and arrays

let biggestNo;

for (let i = 1; i <= 10000; i++) {
  const a = prompt("enter a number");
  if (!a) {
    break;
  } else {
    biggestNo = Math.max(a);
  }
}
console.log(biggestNo);

How do I print 2nd biggest number using this approach without using functions and arrays

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Here's a solution with time complexity of O(n):

const array = [31, 25, 43, 19, 8, 42];

let main = -Infinity, sub = -Infinity;

for (let num of array) {
  if(num > main) {
    sub = main;
    main = num;
  } else if(num > sub) {
    sub = num;
  }
}

console.log('biggest: ' + main);
console.log('second biggest: ' + sub);


Using user input:

let main = -Infinity, sub = -Infinity;

const total = 5;
for(let i = 1; i <= total; i++) {
  const num = +prompt(`Enter a number(${i} of ${total}):`)
  if(num > main) {
    sub = main;
    main = num;
  } else if(num > sub) {
    sub = num;
  }
}

console.log('biggest: ' + main);
console.log('second biggest: ' + sub);

about 4 years ago · Juan Pablo Isaza Report

0

You could use a regular expression to match all numbers collected?

let numbersString;

for (let i = 1; i <= 1000; i++) {
  const a = prompt("enter a number");
  if (!a) {
    break;
  } else {
    numbersString += "-" + a
  }
}

const numbers = numbersString.match(/\d+/g)
const secondLargest = numbers[numbers.length - 2]

console.log(secondLargest);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!