Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function smallestTwoNumbers(arr) {
- let smallest = Number.MAX_SAFE_INTEGER;
- for (let i = 0; i < arr.length; i++) {
- if (smallest > Math.min(smallest, arr[i])) {
- smallest = Math.min(smallest, arr[i]);
- }
- }
- let small = Number.MAX_SAFE_INTEGER;
- for (let i = 0; i < arr.length; i++) {
- if (Math.min(small, arr[i]) < small && smallest < Math.min(small, arr[i])) {
- small = Math.min(small, arr[i]);
- }
- }
- console.log(`${smallest} ${small}`);
- }
- //Втори вариант
- function smallestTwoNumbers(arr) {
- if (arr.length >= 3) {
- let smallest = Number.MAX_SAFE_INTEGER;
- for (let i = 0; i < arr.length; i++) {
- if (smallest > Math.min(smallest, arr[i])) {
- smallest = Math.min(smallest, arr[i]);
- }
- }
- let small = Number.MAX_SAFE_INTEGER;
- for (let i = 0; i < arr.length; i++) {
- if (Math.min(small, arr[i]) < small && smallest < Math.min(small, arr[i])) {
- small = Math.min(small, arr[i]);
- }
- }
- console.log(`${smallest} ${small}`);
- } else if (arr.length == 2){
- console.log(`${Math.min(arr[0], arr[1])} ${Math.max(arr[0], arr[1])}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement