<function decoration/statment>

function greet(){
    //do somthing
    console.log('hello');
} 
greet();

for (let i=0; i<50; i++){
    funcName();
}


// dice roll function
function rollDi(){
    let roll = Math.floor(Math.random()*6)+1;
    console.log(`Rolled: ${roll}`);
}
rollDi();

function throwDice(){
    rollDi();
    rollDi();
    rollDi();
    rollDi();
}

 

 

 

<funtion argument>

: 변수를 받아들이는 함수

//no inputs
'hello'.toUpperCase();

//argumets
'hello'.indexOf('h'); //0
'hello'.indexOf('o'); //4


//greet take2
function greet(person){
    console.log(`Hi, ${person}!`);
}
greet('Ned');
greet('Maria');

// dice roll function 2
function rollDi(){
    let roll = Math.floor(Math.random()*6)+1;
    console.log(`Rolled: ${roll}`);
}


function throwDice(numRolls){
    for(let i =0; i<numRolls; i++){
       rollDi();
    }
}

 

 

 

<function w/ multiple arguments>

function square(num, num2){
    console.log(num*num2);
}

function sum(x, y){
    console.log(x + y);
}

function div(a, b){
    console.log(a / b);
}

 

 

 

<return statment>

: return statment is function execution AND specifies VALUE TO BE RETURNED!!!

function add(x, y){
    return x + y;// 리턴문이 실행되는 즉시 이 코드는 여기에서 멈춤
    console.log('all done');//<- this code is NOT gonna be worked.
}
const total = add(4,5);
total; //9


//v1
function isPurple(color){
    if(color.toLowerCase() === 'purple'){
        return true;
    }else{
        return false;
    }
}

//v2
function isPurple(color){
    if(color.toLowerCase() === 'purple'){
        return true;
    } 
    return false;
}

//v3
function isPurple(color){
    return color.toLowerCase() === 'purple'; //boolean statment
}




function containsPurple(arr){
    for(let color of arr){
        if(color === 'purple' || color === 'magenta'){
            return true;
        }
        //return false;// 이지점에서는 루프를 한번만 돌았기 때문에 모든 배열에 퍼플이나 마젠타가 있는지 확인불가능
    }
    return false;// 때문에 모든 루프가 끝난 이지점에서 리턴을 선언해야함
}

 

 

 

<Practice>

 

//write function to check valid password

// over 8 characters

// not have space

// not have username

//v1
function isValidPassword(password, username) {
    if (password.length < 8) {
        return false;
    }
    if (password.indexOf(' ') !== -1) {
        return false;
    }
    if (password.indexOf(username) !== -1) {
        return false;
    }
    return true;
}

//v2
function isValidPassword(password, username) {
    if (
        password.length < 8 ||
        password.indexOf(' ') !== -1 ||
        password.indexOf(username) !== -1
    ) {
        return false;
    }
    return true;
}

//v3
function isValidPassword(password, username) {
    const tooShort = password.length < 8;
    const hasSpace = password.indexOf(' ') !== -1;
    const hasUsername = password.indexOf(username) !== -1;
    if (tooShort || hasSpace || hasUsername) return false;
    return true;
}

//v4
function isValidPassword(password, username) {
    const tooShort = password.length < 8;
    const hasSpace = password.indexOf(' ') !== -1;
    const hasUsername = password.indexOf(username) !== -1;
    return !tooShort && !hasSpace && !hasUsername;
}

 

 

// wirte a function to find the avg value in array of numbers

function getAvg(arr) {
    let total = 0;
    for (let num of arr) {

        total += num;
    }
    let avg = total / arr.length;
    return avg;
}

 

 

// isPangram

function isPangram(sen) {
    let lowerCased = sen.toLowerCase();
    for (let char of 'abcdefghijklmnopqrstuvwxyz') {
        if (lowerCased.indexOf(char) === -1) {
            return false;
        }
    }
    return true;
}

 

 

// random card select

function pick(arr) {
    let rdPick = Math.floor(Math.random() * arr.length);
    return arr[rdPick];
}

function getCard() {
    const num = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
    let rdNum = pick(num);
    const sign = ['hearts', 'spades', 'clubs', 'diamonds'];
    let rdSign = pick(sign);

    console.log(`{${rdNum}, ${rdSign}}`);

}