Tuesday, February 14, 2023

Coding Challenge #2

 Daily Coding Problem #1 2023-02-13

Problem: Given an array of numbers and second number N, check whether any two of the numbers in the list add up to number N.

// ary of random size [3,33] with random numbers in range [0,30]
var a = [];
function makeAry(){
    var o = [];
    var cuanto = Math.round(Math.random() * 30) + 3;
    for(var xint = 0;xint < cuanto;xint++)
        o.push(Math.floor(Math.random() * 30));
    return o;
}
function ran(max){
    return Math.round(Math.random() * max);
}
function check(n){
    for(var xint = 0, len = a.length;xint < len;xint++){
        for(var yint = 0, len2 = len;yint < len2;yint++){
            if(xint == yint) continue;
            if(a[xint] + a[yint] == n){
                console.log("Math: xint(" + a[xint] + ") + yint(" + a[yint] + ") = n(" + n + ")");
                return true;
            }
        }
    }
    return false;
}
function test(n){
    if(check(n)) console.log("Success! Two of the " + a.length + " numbers added up to " + n + "!");
    else console.log("None of the " + a.length + " numbers added up to " + n + ".");
}

a = makeAry();
test(ran(50));
test(ran(50));
test(ran(50));

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...