<script>
//BEAN COUNTING 2
var string = prompt("Enter your string");
var character = prompt("Enter character which should be counted");
var count = 0;
function countChar(string, character){
for (N=0; N < string.length; N++){
if (string[N] === character){
count++
}
}
console.log(count);
}
countChar(string, character);
</script>
Recursive function example:
function isEven(num) {
num = Math.abs(num);
if (num == 0) { return true; }
else if (num == 1) { return false; }
else {
num = num - 2;
return isEven(num);
}
}
isEven(2);
BEAN COUNTING
function countBs(chars, letter) {
let amount = chars.length;
let count = 0;
for (i = 0; i < amount; i++) {
if (chars[i] == letter) {
count++
}
}
return count;
}
countBs("Boys", "s");
1.function min(a, b){
if(a<b){
return a;
}else{
return b;
}
return min(a,b);
}
2.
function isEven(number){
if(number==0){
return true;
}else if(number ==1){
return false;
}else if (number<0){
return isEven(number+2);
}else{
return isEven(number-2);
}
}
}
3.
function countBs(str){
function countChar(n,Bs){
x=str.length;
if (n==x){
return Bs ;
}else if(str[n]=='B'){
return countChar(n+1, Bs+1);
}else {
return countChar(n+1, Bs);
}
}
return countChar(0, 0) ;
}
Exercises
Minimum:
function getMin(n1, n2) {
let minNbr;
if (n1 < n2) {minNbr=n1;} else {minNbr=n2;};
return minNbr;
};
Recursion:
function isEven(nbr) {
if (nbr == 0) {
return true;
} else if (nbr == 1) {
return false;
} else if (nbr < 0) { return "Not a valid nbr!!!";};
} else {
return isEven(nbr-2);
};
};
Beans:
function countBs (str) {
let cnt = 0;
for (i=0; i<str.length; i++) {
if (str[i] == "B") {cnt++;};
};
return cnt;
};
countBs("alibaBBa");
function countChar(str, char) {
let cnt = 0;
for (i=0; i<str.length; i++) {
if (str[i] == char) {cnt++;};
};
return cnt;
};
Minimum:
// Your code here.
function min(a,b) {
if (a < b) {return a;}
else {return b}
}
console.log(min(0, 10));
// → 0
console.log(min(0, -10));
// → -10
Recursion:
// Your code here.
function isEven(n) {
if (n == 0) return true;
else if (n == 1) return false;
else if (n < 0) return isEven(-n);
else return isEven(n - 2);
}
console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → ??
Bean Counting:
// Your code here.
function countBs(string) {
return countChar(string, “B”);
}
function countChar(string, char) {
let count = 0;
for (let a = 0; a < string.length; a++) {
if (string[a] == char) {
count += 1;
}
}
return count;
}
console.log(countBs(“BBC”));
// → 2
console.log(countChar(“kakkerlak”, “k”));
// → 4
// function Math.max takes any amount of arguments and gives the largest.
console.log(Math.max(2,4));
// function Math.min takes two arguments and returns smallest.
console.log(Math.min(2,4));
// write a function min that takes two arguments and gives the minimum
function min(a,b){
if(a < b) {
return a;
}
else {
return b;
}
}
console.log(min(4,5));
// create a function to accept a single parameter and return a Boolean.
function isEven(n) {
if(n == 0){
return true;
}
else if(n == 1){
return false;
}
else if(n < 0){
return isEven(-n);
}
else {
return isEven(n - 2);
}
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));
FIND MINIMUM
function minimum(a,b){
if ((a-b) < 0){
return a;
}else {
return b;
}
}
var a = Number(prompt("Which is the first number? "));
var b = Number(prompt("Which is the second number? "));
alert("The minimum number is " + minimum(a,b));
EVEN/ODD
function isEven(n){
var num = n;
while(n >= 0){
if(n == 0){
alert(num + " is EVEN");
}
if (n==1) {
alert(num + " is ODD");
}
n = n-2;
}
}
let n = Number(prompt("Give me a number: "));
if (n < 0) {
alert(n + " is a negative number");
}else {
isEven(n);
}
BEAN COUNTING
function countLetter(w,l){
letterCount = 0;
for (var i = 0; i < w.length; i++) {
if (w[i] == l) {
letterCount++;
}
}
alert("The word -" + w + "- has " + letterCount + " " + l + " charachters.");
}
let word = prompt("Write a word: ");
let letter = prompt("Which letter do you want to count? ");
console.log(word);
countLetter(word,letter);
// Chapter 3 exercise 1
function MinNumber(a,b){
if(a<b){
document.write (a)
}
else{
document.write(b)
}
}
MinNumber(4,45);
// Chapter 3 exercise 2
function isEven(n) {
if (n == 0) return “even”;
else if (n == 1) return “odd”;
else if (n < 0) return isEven(-n);
else return isEven(n - 2);
}
console.log(isEven(30));
// Chapter 3 exercise 3
function countBs(string){
var count = 0;
for (var a = 0; a < string.length; a++){
if (string[a] == “B”) {
count += 1;
}
}
}
countBs(“BBdesf”);
MINIMUM
function min(a,b){
if (a<b){
return a;
} else {
(a>b)
return b ;
}
}
RECURSION
My first solution was to solve the exercise with the remainder operator.
function isEven(N){
if (N%2 == 0) {
return “even”;
else
return “odd”;
}
}
…but after reading the question again … I came up with this solution.
function isEven(n){
if (n === 0){
return true;
} else if { n === 1){
return false;
} else {
return isEven(n-2);
}
}
BEAN COUNTING
function countBS(string){
let count = 0;
for (let n = 0; n < string.length ; n +=1){
if (string.charAt(n) === “B”)
count +=1;
}
return count;
}
function countChar(string,c){
let count = 0;
for (let n = 0; n < string.length; n+=1){
if (string.charAt(n) === c )
count +=1;
}
return count;
}
Exercise 1
function min(x,y){
if (x<y){
return x;
}else return y;
}
console.log(min(3,5));
console.log(min(5,3));
Exercise 2
function isEven(n){
if (n==0){
return true;
}else if (n==1){
return false;
}else if (n<0){
return isEven(-n);
}else return isEven(n-2);
}
console.log(isEven(75));
console.log(isEven(50));
console.log(isEven(-1));
Exercise 3
function countChar(string, ch){
let count = 0;
for (i=0; i<string.length; i++){
if (string[i] == ch){
count += 1;
}
}
return count;
}
function countBs(string){
return countChar(string,“B”);
}
console.log(countBs(“BarakaB”));
console.log(countChar(“BarakaB”,“k”));
I had to look at the answers for the last one, it took me several attempts and several reads through the material to understand it but I get it now!!
Q 1. “Write a function min that takes two arguments and returns their minimum.”
ANS:
function isMin(w, x) {
if(w < x)
return w;
else return x;
}
console.log(isMin(205, 306));
console.log(isMin(2432, 1805));
// 205
// 1805
Q 2. Write a recursive ‘isEven’ function that accepts a single number parameter and returns a boolean.
ANS:
function isEven(num){
if(num == 0)
return true;
else if(num == 1)
return false;
else if(num < 0)
return isEven(-num);
else
return isEven(num-2);
}
// test # 1 (negative 1)
console.log(isEven(50))
= true
console.log(isEven(75))
= false
console.log(isEven(-1))
= false
// test # 2 (negative 2)
console.log(isEven(50))
= true
console.log(isEven(75))
= false
console.log(isEven(-2))
= true
Q 3. write a function called countChar that behaves like countBs, except
it takes a second argument that indicates the character that is to be counted
(rather than counting only uppercase “B” characters). Rewrite countBs to
make use of this new function.
ANS:
function countChar(string, ch) {
var counted = 0;
for(var i = 0; i < string.length; i++)
if(string.charAt(i) == ch)
counted += 1;
return counted;
}
function countBs(string) {
return countChar(string, "B")
}
console.log(countBs("BbbBBbBBbbBBBbbB"));
= 9
console.log(countChar("jjjJJJjjJjjJjJjjjJjJj", "J"));
= 8
1. min() function:
if (a <= b) return a;
else return b;
}
console.log(min(-3,4)); //output: -3
2. Recursion:
function isEven(n){
if(n == 0) return true;
else if (n == 1) return false;
else if (n < 0) return isEven(-n);
else return isEven(n-2);
}
console.log(isEven(30));
console.log(isEven(57));
console.log(isEven(-81));
// Output: True, False, False
3. Bean Counting:
function countBs(str,char)
{
//str = toString(str);
let counter=0;
for (let i=0; i < str.length; i++)
{
if (str[i]== char) counter++;
}
return counter;
}
console.log(countBs("BBDHJJLKJJFBBWKEHGBBEBB" , "B"));
// Output: 8
/*
1.
function findMin(x,y,z){
if (x > y) {
return z + y + 5;
}
else
return x + z + 3;
}
console.log(findMin(6,3,2));
2.
function isEven(n){
if (n % 2 === 0){
return true;
} else {
return false;
}
}
console.log(isEven(50));
console.log(isEven(0));
console.log(isEven(-1));
3.
function countBs(string){
let result = 0
for (let i = 0; i <= string.length; i++){
if (string[i] === ‘B’){
result = result + 1
}
}
return result;
}
function countChar(string, character){
let result = 0
for (let i = 0; i <= string.length; i++){
if (string[i] === character){
result = result + 1
}
}
return result;
}
console.log(countBs(“BoBBheBuilBer”));
console.log(countChar(“llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch”, “l”));
*/
Minimum
function minFunction(a, b) {
var toReturn = 0;
if(a < b) {
toReturn = toReturn + a;
}
if(b < a) {
toReturn = toReturn + b;
}
return toReturn;
}
var testMinFunction = minFunction(3, 5);
console.log(testMinFunction); // returns 3
Recursion
function isEven(n) {
if (n == 0) return true;
else if (n == 1 || n == -1) return false;
else return isEven(n - 2);
}
console.log(isEven(-1)); // -1 fix returns “false”
Bean Counting (I was not able to figure this one out but here is what I did)
var str = “BeN”;
//var n = str.length; // this maybe the answer for the first part of the problem
var numStore = (str.charAt(0) + str.charAt(1)) + str.charAt(2);
console.log(numStore);
function minFunction(a, b, c) {
var toReturn = “”;
if (a < b && c < b) {
toReturn = toReturn + a + c;
}
else {
toReturn = toReturn + b;
}
return toReturn;
}
// this gets the smallest number which are the capital letters
var answer = minFunction(str.charAt(0), str.charAt(1), str.charAt(2));
console.log(answer); // returns “BeN” and “BN”