Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
11 kB
3
Indexable
Never
let message;

function greeting() {
    let currentHour = new Date().getHours();
    if (currentHour < 6) {
        message = "Bonne nuit";
    } else if (currentHour < 12) {
        message = "Bonne matin";
    } else if (currentHour < 14) {
        message = "Bon midi";
    } else if (currentHour < 18) {
        message = "Bon apres-midi";
    } else {
        message = "Bonne soiree";
    }
    return message;
}

document.getElementById("greeting").textContent = greeting();
document.getElementById("hrsmin").textContent = new Date().getHours() + "h:" + new Date().getMinutes() + "m";

document.getElementById("rolldice").onclick = function () {
    document.getElementById("diceresult").textContent = Math.floor((Math.random() * 6) + 1);
}

function isChecked(){
    if (document.getElementById("minmax").checked) {
        document.getElementById("minmaxsimpleform").style.display = "block";
    } else if (!document.getElementById("minmax").checked) {
        document.getElementById("minmaxsimpleform").style.display = "none";
    }
}

function showtimestext(){
    if (document.getElementById("normalthrow").checked) {
        document.getElementById("multiplethrowcontainer").style.display = "none";
        document.getElementById("probabilityofpile").textContent = "";
        document.getElementById("resultatmultiplethrow").textContent = "";
    } else {
        document.getElementById("multiplethrowcontainer").style.display = "block";
        document.getElementById("resultcoinflipgame").textContent = "";
    }
}

document.getElementById("generatenumber").onclick = function() {
    let min = Number(document.getElementById("minimum").value);
    let max = Number(document.getElementById("maximum").value);
    if (min > max) {
        alert(`Minimum cannot be bigger than max: ${min} > ${max}`);
    } else {
        let genrandomnumber = Math.floor((Math.random() * (max - min + 1)) + min);
        document.getElementById("randomnumber").textContent = genrandomnumber;
    }
}

document.getElementById("coinflipbutton").onclick = function() {
    if (!document.getElementById("multiplethrow").checked) {
        let result = Math.floor((Math.random() * 2) + 1);
        if (result == 1) {
            document.getElementById("resultcoinflipgame").textContent = "Result: face";
        } else {
            document.getElementById("resultcoinflipgame").textContent = "Result: pile";
        }
    } else {
        let amountofthrows = Number(document.getElementById("nbofmultiplethrow").value);
        let nbpiles = 0;
        let tableauresultat = [];
        for(let i=0; i<amountofthrows; i++){
            let multresult = Math.floor((Math.random() * 2) + 1);
            if (multresult == 2) {
                nbpiles++;
                tableauresultat.push("pile");
            } else {
                tableauresultat.push("face");
            }
        }
        let probabilityofpile = (nbpiles * 100)/amountofthrows;
        document.getElementById("probabilityofpile").textContent = "Probability of piles: " + probabilityofpile;
        document.getElementById("resultatmultiplethrow").textContent = "Resultat: " + tableauresultat;
    }
}

document.getElementById("calculatebuttonfraction").onclick = function() {
    let fractionstringform = document.getElementById("fractioninput").value;
    let arrstrg = fractionstringform.split("/");
    let nbofslash = 0;
    for (let i=0; i<fractionstringform.length; i++){
        if (fractionstringform.charAt(i) == "/") {
            nbofslash++;
        }
    }
    for (let i=0; i<arrstrg.length; i++){
        if (isNaN(Number(arrstrg[i]))){
            alert('Make sure both a and b are numbers');
            throw new Error('Make sure both a and b are numbers');
        }
    }
    if (arrstrg.length == fractionstringform.length || arrstrg.length == 1 || fractionstringform.charAt(0) == "/" || fractionstringform.charAt(fractionstringform.length-1) == "/" || nbofslash > 1) {
        alert('Respect the format a/b');
        throw new Error('Respect the format a/b');
    }
    if (Number(arrstrg[1]) == 0){
        alert('Cant divide by 0');
        throw new Error('Cant divide by 0');
    }
    document.getElementById("resultfraction").textContent = Number(arrstrg[0])/Number(arrstrg[1]);
}

document.getElementById("arraybutton").onclick = function() {
    document.getElementById("normalarray").textContent = "";
    document.getElementById("inversedarray").textContent = "";
    document.getElementById("forofarray").textContent = "";
    document.getElementById("arraysettings").style.display = "block";
    let arraystrg = document.getElementById("inputarray").value;
    let arrayintg = arraystrg.split(",");
    for (let i=0; i<arrayintg.length; i++){
        if (i<arrayintg.length - 1){
            document.getElementById("normalarray").textContent = document.getElementById("normalarray").textContent + arrayintg[i] + ",";
        } else {
            document.getElementById("normalarray").textContent = document.getElementById("normalarray").textContent + arrayintg[i];
        }
    }
    for (let i=arrayintg.length-1; i>=0; i--){
        if (i>0){
            document.getElementById("inversedarray").textContent = document.getElementById("inversedarray").textContent + arrayintg[i] + ",";
        } else {
            document.getElementById("inversedarray").textContent = document.getElementById("inversedarray").textContent + arrayintg[i];
        }
    }  
    for (const element of arrayintg){
        document.getElementById("forofarray").textContent = document.getElementById("forofarray").textContent + element + ",";
    }
}

document.getElementById("arraybuttonmax").onclick = function(){
    document.getElementById("arraysettingsmax").style.display = "block";
    let arraystrg = document.getElementById("inputarray").value;
    let arrayintg = arraystrg.split(",");
    if (arrayintg.length == 0){
        document.getElementById("maxcalculationofarray").textContent = "";
    } else {
        let max = Number(arrayintg[0]);
        for (let i=1; i<arrayintg.length; i++){
            if (Number(arrayintg[i]) > max){
                max = Number(arrayintg[i]);
            }
        }
        document.getElementById("maxcalculationofarray").textContent = max;
    }
}

document.getElementById("buttoncoolfeature").onclick = function(){
    document.getElementById("coolfeature").style.display = "block";
    document.getElementById("solutionforcoolfeature").textContent = "";
    let stringarrs = document.getElementById("inputarray").value;
    let intgarr = stringarrs.split(",");
    for (let i=0; i<=intgarr.length; i++){
        if (Number(intgarr[i]) % 2 == 0){
            let temp = intgarr[i];
            intgarr.splice(i,1);
            intgarr.unshift(temp);
        }
    }
    document.getElementById("solutionforcoolfeature").textContent = intgarr;
}

document.getElementById("itsplaceitsvaluebutton").onclick = function(){
    document.getElementById("containeritsplaceitsvalue").style.display = "block";
    let stringarr = document.getElementById("arrayitsplaceitsvalue").value;
    let intgarr = stringarr.split(",");
    let value = Number(document.getElementById("numberitsplaceitsvalue").value);
    if (value < 0){
        alert("Value is negative, cant place values at negative positions in array");
        throw new Error("Value is negative, cant place values at negative positions in array");
    }
    intgarr[value-1] = value;
    if (document.getElementById("fillvoidwith0s").checked){
        for (let i=0; i<intgarr.length; i++){
            if (intgarr[i] == null){
                intgarr[i] = 0;
            }
        }
    }
    document.getElementById("resultitsplaceitsvalue").textContent = intgarr;
}

document.getElementById("executemultipleinsertion").onclick = function(){
    let strgarr = document.getElementById("arraymultipleinsertion").value;
    let intgarr = strgarr.split(",");
    let posvalue = document.getElementById("positionmultipleinsertion").value;
    let value = document.getElementById("valuemultipleinsertion").value;
    let repetition = document.getElementById("repetitionmultipleinsertion").value;
    document.getElementById("containerofresult").style.display = "block";
    let d = intgarr.length;
    for (let i=d-1; i>=Number(posvalue); i--){
        intgarr[i+Number(repetition)] = intgarr[i];
    }
    for (let i=posvalue; i<Number(repetition)+Number(posvalue); i++){
        intgarr[i] = value;
    }
    if (document.getElementById("fillvoidwith0sm").checked){
        for (let i=0; i<intgarr.length; i++){
            if (intgarr[i] == null){
                intgarr[i] = 0;
            }
        }
    }
    document.getElementById("resultmultiplethrow").textContent = intgarr;
}

function table(x,n = 10){
    for (let i=1; i<n+1; i++){
        console.log(`${x}*${i}=${x*i}`)
    }
}

function carre(val){
    return val*val;
}

function transformator(array,callBack){
    for(let element of array){
        element = callBack(element);
        console.log(element);
    }
}
function maxof2(a,b){
    if (a > b){
        return a;
    } else {
        return b;
    }
}

function argsvariables(...args){
    if (args.length == 1){
        if(args[0] >= 0){
            return Math.sqrt(args[0]);
        } else {
            return undefined;
        }
    } else if (args.length == 2){
        return maxof2(args[0],args[1])
    } else if (args.length == 3){
        if ((args[0] >= args[1] && args[2] >= args[1]) || (args[0] <= args[1] && args[2] <= args[1])){
            return false;
        } else {
            return true;
        }
    } else {
        alert("Introduce at max 3 arguments");
        throw new Error("Introduce at max 3 arguments");
    }
}

function zero(){
    return 0;
}

function inc(a){
    return function(){
        return a() + 1;
    }
}

function appeldynamique(array){
    let fixedarr = [];
    for (let i=0; i<array.length; i++){
        if (typeof array[i] === "function"){
            fixedarr.push(array[i]())
        }
    }
    return fixedarr;
}

const tab = [function(){return 2+2;},2,true,function(){return true;}]
const result1 = appeldynamique(tab);
console.log(result1);

const result = inc(inc(inc(inc(zero))));
const resultat = result();
console.log(resultat);
console.log(result);

let array = [1,2,3,4,5,6,7,8,9,10];
transformator(array,carre);

table(8);

console.log(argsvariables(4));
console.log(argsvariables(-5));
console.log(argsvariables(5,7));
console.log(argsvariables(2,2));
console.log(argsvariables(1,5,10));
console.log(argsvariables(2,5,3));
console.log(argsvariables(10,5,7));
console.log(argsvariables(2,5,5));
console.log(argsvariables(5,5,2));
console.log(argsvariables(4,5,6));