'use strict'
// All the exercises should use while loop:

// Please be sure to connect this file to another HTML file

// Don’t search on the internet except if it is written in the question

// You should solve all the question by what you understand from the lecture
// and with your pair not solo

// And don’t look or use a code that you wrote before 

console.log('Here is : While');
// document.write("<h4>Solutions in console part</h4>");
document.getElementById('p13').innerHTML="<h2>Solutions in console part</h2>";

/*1
Create a function called subtract
that takes a single parameter n,
and return the subtraction of all
starting from n to 0
Ex: subtract(2); => 2 - 1 - 0 => 1
Ex: subtract(5); => 5 - 4 - 3 - 2 - 1 - 0 => -5
Ex: subtract(9); => 9 - 8 - 7 - 6 - 5 - 4 - 3 - 2 - 1 - 0 => -27
*/
function subtract(num){
    let sub=num;
    let i=num-1;
    while(i>0){
            sub=sub-i;
            i--;
        }
    
    return sub;
}
console.log('Q1 ' ,subtract(9));
document.getElementById('p1').innerHTML="Q1: subtract(9) is " + subtract(9);
/*
2
Create a function called factorial
that takes a single parameter n,
and return the product of all integers up to n starting from 1
Ex: factorial(2); => 2 * 1 => 2
Ex: factorial(4); => 4 * 3 * 2 * 1 => 24
*/

function factorial(num){
    let mutiple=1;
    while(num>0){
        mutiple=num*mutiple;
        num--;
    }
    return mutiple;
}

console.log('Q2 ' ,factorial(4));
document.getElementById('p2').innerHTML="Q2: factorial(4) is " + factorial(4);

/*3Create a function called repeatStr
that takes 2 parameters string and number,
and return the string number time with space
Important: the continue condition should be [somthing !== 0]
** Think how we can return nothing ???
** Important: You should dint have extra space at the end
Ex: repeatStr("to",2); => "to to"
Ex: repeatStr("to",4); => "to to to to"
*/

function repeatStr(string,number){
    let lastString='';
    while (number!== 0){
        lastString=lastString+string;
        lastString=lastString+ ' '
        number--;
    }
    return lastString
}
console.log('Q3 ' ,repeatStr("to",4));
document.getElementById('p3').innerHTML="Q3: repeatStr('to',4) is " + repeatStr("to",4);



/*
4
Create a function called sum2
that take two parameter
and will return the sumation from the first number to the second number
Ex: sum2(4, 5); => 4 + 5 => 9
Ex: sum2(3, 6); => 3 + 4 + 5 + 6 => 18
*/


function sum2(num1,num2){
    let sum=0;
    // First case when num1>num2
    while(num1>num2){
        sum=sum+num1;
        num1--;
    }
    while(num2>=num1){
        sum=sum+num2;
        num2--;
    }
    // Second case when num2>=num1
 return sum;
}
console.log('Q4 ' ,sum2(3, 6));
document.getElementById('p4').innerHTML="Q4: sum2(3, 6) is " + sum2(3, 6);


/*
5
Create a function called repeatStr2
that takes 2 string parameters,
and return the first string number time
(of the lenght of the secend string) with space betweeen it
** Important: the continue condition should be [somthing!== 0]
** Important: You should't have extra space at the end
Ex: repeatStr2("ro","cc"); => "ro ro"
Ex: repeatStr2("ro","fff"); => "ro ro ro"
*/

function repeatStr2(string,string_num){
    let lastString='';
    let num=string_num.length;
    while (num !==0){
        lastString=lastString+' '+string;
        num--;
    }
    return lastString
}

console.log('Q5 ' , repeatStr2("ro","fff"));
document.getElementById('p5').innerHTML="Q5:  repeatStr2('ro','fff') is " +  repeatStr2("ro","fff");


/*
6
Create a function called multiOf
that takes 3 parameters,
and return the first number mutiple by
(the second one ^ the third number)
** Important: dont use **
Ex: multiOf(4,10,3); => 4000
Ex: multiOf(6,3,2); => 54
Ex: multiOf(6,2,3); => 48
*/

function multiOf(num1,num2,num3){
    let result;
    while(num3){
        result=Math.pow(num2,num3);
        // console.log(result);
        result=result*num1;
        break;
    }
    return result;
}

console.log('Q6 ' , multiOf(6,2,3));
document.getElementById('p6').innerHTML="Q6:  multiOf(6,2,3); is " +  multiOf(6,2,3);


/*
7
Create a function called muti2
that take two parameter
and will return the multiplication
from the first number to the second number
Ex: muti2(4, 5); => 4 * 5 => 20
Ex: muti2(3, 6); => 3 * 4 * 5 * 6 => 360
*/

function muti2(num1,num2){
    let mutiple=1;
    while(num2>=num1){
        mutiple=mutiple*num1;
        num1++;
    }
 return mutiple;
}

console.log('Q7 ' , muti2(3, 6));
document.getElementById('p7').innerHTML="Q7:  muti2(3, 6); is " +  muti2(3, 6);


/*
8
Create a function called numberBetweenUs
that take 2 parameters
and return the number between them
** Important: You should't have extra space at the end and comma
** Important: the stop condition should [num1!==num2-1]
** hint: maybe you will have 2 if statment
numberBetweenUs(2,8) => "3, 4, 5, 6, 7"
numberBetweenUs(1,3) => "2"
*/

function numberBetweenUs(num1,num2){
    let array=[];
    while(num1!==num2-1){
      array.push(num1+1);
      num1++;
    }
    return `"${array.join(', ')}"`;
} 

console.log('Q8 ' , numberBetweenUs(1,3));
document.getElementById('p8').innerHTML="Q8:  numberBetweenUs(1,3); is " +  numberBetweenUs(1,3);


/*
9
Write a function called countDown
that takes one parameter
and return a string represent the count down from the number
that insert to 0
instead of 0 you shoulf have "done"
between each number a comma and space
** you need to use recursion
** Important: You should't have extra space at the end and comma
countDown(5)
=> "5, 4, 3, 2, 1, done"
countDown(2)
=> "2, 1, done"
countDown(7)
=> "7, 6, 5, 4, 3, 2, 1, done"
*/

function countDown(num){
    let array=[];
    while(num>0){
        array.push(num);
        num--;
    }
    if(num==0){
        array.push('done');
    }
    return `"${array.join(', ')}"`;
}

console.log('Q9 ' , countDown(7));
document.getElementById('p9').innerHTML="Q9:  countDown(7); is " +  countDown(7);

/*
10
Write a function called multiplication2
that takes two parameters
and return the multiplication of them
you need to use only sum
multiplication2(5,4) => 20
multiplication2(2,8) => 16
multiplication2(7,6) =>  42
*/

function multiplication2(num1,num2){
    let sum=0;
    while(num2){
       sum=sum+num1;
        num2--;
    }
    return sum;
}
console.log('Q10 ' , multiplication2(7,6));
document.getElementById('p10').innerHTML="Q10:  multiplication2(7,6); is " +  multiplication2(7,6);


/*
11
Write a function called mod2
that takes two parameters
and return the module of them
without using %
mod2(5,4) => 1
mod2(2,8) => 2
mod2(7,4) => 3
mod2(8,4) => 0
*/

function mod2(num1,num2){
    let result;
    while(num2){
        result=(num1-num2 * parseInt(num1 / num2));
        break;
    }
    return result;
}

console.log('Q11 ' ,mod2(8,4));
document.getElementById('p11').innerHTML="Q11: mod2(8,4); is " + mod2(8,4);


/*
12
Write a function called repeatChar
that takes two parameters
and return the times that this char
without case sensetive
repeat inside this string
**hint: "str"[0] => "s"
**hint: "car"[1] => "a"
repeatChar("schOol","o") => 2
repeatChar("school","a") => 0
repeatChar("School","s") => 1
try more case by yourself
*/

function repeatChar(string1,string2){
    let newString1=string1.toLowerCase();
    let string1Length=string1.length;
    let i=0;
    let sum=0;
    while(string1Length>0){
        if(newString1[i]==string2){
            sum=sum+1;
        }
        i++;
        string1Length--;
    }
    return sum;
}
console.log('Q12 ' ,repeatChar("schOol","o"));
document.getElementById('p12').innerHTML="Q12: repeatChar('schOol','o'); is " + repeatChar("schOol","o");
