Giter VIP home page Giter VIP logo

js's Introduction

JavaScript

Training / JavaScript

JavaScript

Exercises

  • Challenges It will maybe hurt a bit, but for your good! 🌡
  • Recall One way to see if you are comfortable with the JS. πŸ“Ό
  • Cookie Clicker We just sell cookies to earn more cookies! πŸͺ

Resources

Links

Simplonline

Mozilla Developer Network

Style Guides

GitHub Awesome Lists

Discourses

To be continued..

Code snippets

Triangle.js

/*
 * Triangle
 *
 * INPUT:
 * - a number N
 *
 * OUPUT: (eg for N=5)
 * #
 * ##
 * ###
 * ####
 * #####
 *
 */

function Line(N) {
    return "#".repeat(N);
}

function Triangle(N) {
    for (var i = 1; i <= N; ++i) {
        console.log(Line(i));
    }
}

Triangle(5);
Triangle(3);

Factorial.js

/*
 * Factorial
 * n! = 1 * 2 * .. * n
 * 
 * 5! = 5 * 4 * 3 * 2 * 1 = 120
 */

// MATHS:
// 0! = 1
// n! = n * (n - 1)!

function fac(n) {
    if (n <= 1) {
        return 1;
    } else {
        return n * fac(n - 1);
    }
}

/* TEST */
fac(5);

Fibonnaci.js

/*
 * INPUT: a number N
 *
 * OUPUT: f(N) where f is the fibonacci function :
 *
 * 0 1 1 2 3 5 8 13 21 34  ..
 *
 * f(0) => 0
 * f(1) => 1
 * f(2) => 0 + 1 = 1
 * f(3) => 1 + 1 = 2
 * f(4) => 1 + 2 = 3
 * f(5) => 2 + 3 = 5
 * f(6) => 3 + 5 = 8
 * f(7) => 5 + 8 = 13
 * ...
 *
 * /!\ MATHS /!\
 *
 *  f(0) = 0
 *  f(1) = 1
 *  f(n) = f(n - 1) + f(n - 2)
 */

function f(N) {
    // PUT YOUR CODE HERE !!!
    // return the Nth number of the fibonnaci sequence
}

// TEST CODE
console.log("Here 50 first fibonnaci numbers");
for (var i = 0; i < 50; ++i) {
    console.log("f(" + i + ") = " + f(i));
}

Fibonnaci - Yvan Bad Solution.js

/*
 * f(0) = 0
 * f(1) = 1
 * f(n) = f(n-1) + f(n-2)
 */

function fib(x) {
    if (x == 0)
        return 0;
    else if (x == 1)
        return 1;
    else
        return fib(x-1) + fib(x-2);
}

/* TEST */

for (var i = 0; i < 100; i++) {
    console.log("fib(" + i + ") = " + fib(i));
}

//
// O(n) = 2^n
// => 2^60 = 1152921504606846976
//

Fibonnaci - Adam Good Solution.js

var T = [];

T[0] = 0;
T[1] = 1;

for (var i = 2; i < 100; i++) {
    T[i] = T[i-2] + T[i-1];
}

function fib(x) {
    return T[x];
}

/* TEST */

for (var i = 0; i < 100; i++) {
    console.log("fib(" + i + ") = " + fib(i));
}

//
// O(n) = n
// => 60
//

CashMachine.js

/* ###### CONSTANT ###### */
var BILLS = [200, 100, 50, 20, 10, 5, 2, 1] // ARRAY of bills with really cheap bills like coins

/* ###### SMART ALGORITHM ###### */
var cash_machine = function (amount) { // FUNCTION
    var i = 0; // iterator
    while (amount > 0) { // check if their is still money to give for the customer
        if (BILLS[i] <= amount) { // try with the larger bill
            console.log("Cash Machine give me a bill of " + BILLS[i] + "€"); // GIVE ME MY MONEY!!!
            amount = amount - BILLS[i]; // reduce the amount of money we still have to gave for the customer
        } else { // try with a smaller bill
            i = i + 1; // increment i variable of 1
        }
    }
}

/* ###### TESTS ###### */

cash_machine(42);
//cash_machine(25);
//cash_machine(128);
//cash_machine(201);
//cash_machine(1024);

CashMachine.js II the Return of the ATM!

var BILLS = [100, 200, 20, 50, 10];

var cash_machine = function (amount) {
    BILLS.sort(function(a, b) { return a < b });
    var i = 0;
    while (amount > 0) {
        if (BILLS[i] <= amount) {
            console.log("Give me a bill of " + BILLS[i] + "€");
            amount = amount - BILLS[i];
        } else if (i < BILLS.length) {
            i = i + 1;
        } else {
            console.log("It lack " + amount + "€");
            break;
        }
    }
}

cash_machine(550);

SetTimeout.js

// PROTOTYPE //
// setTimeout(Function, Number);
///////////////

// Example

// 1.
setTimeout(
    // first parameter
    // (which is an anonymous function without parameter)
    function () { alert("Bouuuuh"); },
    // second parameter
    3000
);

// MATHS //
// g() : is a Function
// f(g, x) : with x is a Number
///////////

// 2.
function test() {
    alert("Bouuuuh");
};

setTimeout(test, 3000);

MapFilter.js

// Definition of Map function
function Map(arr, func) {
    m_arr = [];
    for (var i = 0; i < arr.length; ++i)
        m_arr.push(func(arr[i]));
    return m_arr;
}

// Definition of Filter function
function Filter(arr, func) {
    f_arr = [];
    for (var i = 0; i < arr.length; ++i)
        if (func(arr[i]))
            f_arr.push(arr[i]);
    return f_arr;
}

/** EXAMPLES (with tasty juicy fruits) **/
var T = ["banana", "apple", "strawberry", "melon", "pineapple", "bob"];
console.log(T);
T = Filter(T, function (x) {
    return x.indexOf("a") > - 1;
});
console.log(T);
T = Map(T, function (x) {
    return x + x;
});
console.log(T);
T = Filter(T, function (x) {
    return x.length > 15;
});
console.log(T);
T = Map(T, function (x) {
    return x + "^^";
});
console.log(T);

js's People

Contributors

yvan-sraka avatar

Watchers

James Cloos avatar Adrien avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.