I managed the first 2, but had to look at a few on here for the 3rd and then realised most people have the exact one from the answers page. Made me feel better about struggling though
- Minimum
function Minimum(x,y) {
if (x <= y) {
return x;
}
else if (y < x) {
return y;
}
}
document.write(Minimum(3,9));
- Recursion
function isEven(number) {
if(number < 0){
document.write(“number is negative”);
}
else if(number == 1){
document.write(“number is odd”)
}
else if(number == 0){
document.write(“number is even”)
}
else{
return isEven(number - 2);
}
}
- bean counter
function countBs(string){
let count = 0;
for(let n = 0; n < string.length; n++){
if(string[n]==“B”){
count += 1;
}
}
return count
}
function countChar(string, char){
let count = 0;
for(let n = 0; n < string.length; n++){
if(string[n]==char){
count += 1;
}
}
return count
}
For some reason I can’t get these last ones to print on my browser with document.write(thefunction). Not sure what is missing, as it works on the eloquent JS website
@azalimi I’m not sure on the “HTML1300: Navigation” error, but I can’t see the code describing what you want the function to do.
What I imagine you’re trying to do is something that makes the console say “Hello Azeez!”
which means your function code should read something like this:
function greet(name){
console.log(“Hello” + name + “!”)
}
greet(“Azeez”)
Hope that helps!
Return minimum:
function min(x, y) {
if (x < y) return x;
else return y; }
console.log(min(9, 5));
Answers 5
console.log(min(5, 9));
Answers 5
Recursion:
function isEven(x) {
if (x == 0) return true;
else if (x == 1) return false;
else if (x < 0) return isEven(-x);
else return isEven(x - 2); }
Beancounting:
function countChar(string, ch) {
let counted = 0;
for (let n = 0; n < string.length; n++) {
if (string[n] == ch) {
counted += 1;
}
}
return counted;
}
function countBs(string) {
return countChar(string, “B”);
}
console.log(countBs(“BBcaB”));
=3
console.log(countChar(“supersonicsessionsonsexyslippershoes”, “s”));
=10
Out of curiosity, what is a practical use for the bean counting function?
Function 1 / Minimum
var a, b;
do {
a = Number(prompt("Enter your first number: "));
} while (Number.isNaN(a));
do {
b = Number(prompt("Enter your second number: "));
} while (Number.isNaN(b));
greaterThan(a, b);
function greaterThan(xx, yy){
if (xx > yy) {
document.write(xx + " is greater than " + yy);
}
else if(xx == yy){
document.write(xx + " is equal to " + yy);
}
else {
document.write(yy + " is greater than " + xx);
}
}
Function 2 / Recursion
function isEven(no) {
if (no < 0) {return (isEven(-no)); }
if (no == 0) { return (true); }
if (no == 1) { return (false); }
return (isEven(no-2));
}
Function 2 / Bean counting
function countChar (string, char) {
var count = 0;
for (var i = 0; i < string.length; i++) {
if (string[i] == char) { count++; }
}
return (count);
}
function countBs (string) {
return (countChar(string,“B”));
}
//Minimum of 2 numbers
function min(number1, number2){
if(number1 < number2) return number1;
else return number2;
}
console.log(min(1,2));
//-> 1
//Recursive check for even/odd
function isEven(number) {
number = Math.abs(number);
if (number == 0) return true;
else if (number == 1) return false;
else return isEven(number - 2);
}
console.log(isEven(-50));
//-> true
//count chars
function countChars(inString, whatChar){
let charCounter = 0;
for(i = 0; i < inString.length; i++){
if(inString[i] == whatChar) charCounter++;
}
return () => charCounter;
}
var yourString = "This Beans and Beans function makes sense";
let countes = countChars(yourString, "e");
console.log(countes());
//-> 5
let countBs = countChars(yourString, "B");
console.log(countBs());
//-> 2
EXCERSICISECHAPTER 3
MINIMUM
//public class MinExample1
{
public static void main(String args[])
{
int x = 20;
int y = 50;
//print the minimum of two numbers
System.out.println(Math.min(x, y));
}
}
EXCERSISE CHAPTER 3
Recursion
[quote=“jgmusso, post:7, topic:3080”]
var number = prompt("Number please:");
var msj = isEven(number);
document.write(msj);
function isEven(x) {
if (x == 0) {
return ("Even");
}else if (x == 1) {
return ("Odd");
}else if (x < 0) {
return isEven(-x);
}else {
return isEven(x - 2);
}
}
[/quote]
BEANCOUNTER EXCERSISE - CHAPTER 3
//BEANCOUNTER
function countBs(str) {
var length1 = str.length;
var length2 = str.replace(/B/g,"").length;
return length1 - length2;
}
console.log(countBs(“BBBBBBBBC”));
function countChar(str, char) {
var length1 = str.length;
var regexp = new RegExp(char, “g”);
var length2 = str.replace(regexp,"").length;
return length1 - length2;
}
console.log(countChar(“IVANOOOOOBTECH”, “O”));
//Minimum
//********
function min(a,b){
if (a<b) {
console.log("a is smaller than b");
} else if (a>b) {
console.log("b is smaller than a");
} else if (a==b) {
console.log("They are the same");
} else {
console.log("Cannot compute");
}
}
min(2,3); //a is smaller than b
min(3,2); //b is smaller than a
min(4,4); // They are the same
min(NaN,NaN) //Cannot compute
//Recursion
//*********
function isEven(a){
if (a < 0) { a *=-1; }
if (a == 0) { return true; }
else if (a == 1) { return false; }
else { return isEven(a - 2); }
}
console.log(isEven(0)); //true
console.log(isEven(1)); //false
console.log(isEven(10)); //true
console.log(isEven(-1)); //false
//Bean Counting
//*************
function countChar(string, char){
var numChar = 0;
for (counter = 0 ; counter < string.length; counter ++) {
if (string[counter] == char) { numChar++;}
}
return numChar;
}
function countBs(string) {
return countChar(string, "B");
}
console.log(countBs("sdfBertBadfedfB")); //3
console.log(countChar("dsdfBertBadfeddfBd", "d")); //6
Exercise 1. Minimum
function minNumber (a, b){
return Math.min (a, b);
}
document.write ("Out of all the numbers chosen " + minNumber (a,b) + "was the smallest")
Exercise 2. Recursive
function isEven(n){
//what if the input is negative
if (n < 0) {
n = Math.abs(n);
}
if (n === 0){
return true;
}
if (n === 1){
return false;
}
return isEven (n - 2);
}
console.log(isEven(50))
console.log(isEven (75))
Exercise 3. Beancounting
function countBs (string) {
// return a call to countChar with input and “B”
return countChar(string, ‘B’);
}
function countChar (string, character){
// creat a result, set to 0
let result = 0;
//loop over the string
for (let i = 0; i < string.length; i++) {
//if current character matches input character
if (string [i] === character){
result ++;
//increment out result by 1
}
}
//return result
return result;
}
console.log (countBs(“BBC”));
console.log (countChar (“kakkerlak”, “k”));
Homework on Chapter 3 Exercise - ,September 10th 2020
- Minimum
function min(a, b) {
if (a < b) return a;
else return b;
}
console.log(min(0, 10));
console.log(min(0, -10));
- 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(50));
console.log(isEven(75));
console.log(isEven(-1));
3. Bean Counting
function countChar(string, ch) {
let counted = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == ch) {
counted += 1;
}
}
return counted;
}
function countBs(string) {
return countChar(string, "B");
}
console.log(countBs("BBC"));
console.log(countChar("kakkerlak", "k"));
//Find if a number is even or odd//
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);
}
document.write(isEven(78));
//Count the number of "B"s in a string and print the result//
function CountBs(string){
let count = 0; for(i = 0; i < string.length; i ++){
if (string[i]==“B”){
count++;
}
}
return count}
document.write(CountBs(“BBBBOOKUJHBHBHBB”));
//This was not part of the assignment but thought it was good to know that if you did not choose “==” but choose “=” it would count character of the same type. And this would give you a different answer.//
function CharBs(stringB){
let countChar= 0; for(i = 0; i < stringB.length; i ++){
if (stringB[i]=“B”){
countChar++;}
}
return countChar}
document.write(CharBs(“BBBJBJBOBBYUIJBBHGRBB”));
//This is a function that will act like the above countBs function except it will take two parameters and the second parameter is call “Char” and this parameter will allow the user to select what character he wants to be counted instead of the letter “B” being the only one you can choose.//
function Charany(Countany,Char){
let counter = 0; for(i = 0; i < Countany.length; i++){
if(Countany[i]== Char){
counter++}
}
return counter}
document.write(Charany(“UHZZZHHJJJJJJJJJJJJJRYHZZZTJJ”,“J”));
function min(x, y)
{
if (x > y)
{
return x;
}
else
{
return y;
}
}
console.log(min(0, 10)); //10
console.log(min(0, -10)); //0
function isEven(x)
{
if (x % 2 == 0 || x % 2 == -0) return true;
else return false;
}
console.log(isEven(-10)); //true
console.log(isEven(7)); //false
function countChar(text, target)
{
var count = 0;
for (var charIndex = 0; charIndex < text.length; charIndex++)
{
if (text[charIndex] == target)
{
count++;
}
}
return count;
}
console.log(countChar("BBaabbaaBb", 'B')); //3
-
Minimum
function min(x,y) {
if (x < y) return x;
else return y;
}
console.log(min(2,4)); -
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);
} -
Bean counting
function countBs(string) {
let count = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == “B”) {
count += 1;
}
}
}
function countChar(string, chrar) {
let count = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == char)
count++;
}
return count;
}
function countBs(string)
{
return countChar(string, ‘B’);
}
minimum
function min(a,b){
var diff=a-b;
if(diff<=0){
return a;
}
else{return b;}
}
alert(min(5,8));
recursion
function isEven(a){
var remainder=a;
while(!(remainder<=1)){
remainder-=2;
}
if(remainder){
return false;
}
else{return true}
}
alert(isEven(50));
Fixed for negative numbers
<script>
function isEven(a){
var remainder=a;
if(a<0){
while(!(remainder>=-1)){
remainder+=2;
}
if(remainder){
return false;
}
else{return true}
}
else{
while(!(remainder<=1)){
remainder-=2;
}
if(remainder){
return false;
}
else{return true}
}
}
alert(isEven(-1));
</script>
Bean counting
countBs
function countBs(string){
var count=0;
for (var i = 0; i < string.length; i++) {
if( string[i]=="B"){count++;}
}
return count;
}
alert(countBs("BABY"));
countChar
function countchar(string,target){
var count=0;
for (var i = 0; i < string.length; i++) {
if( string[i]==target){count++;}
}
return count;
}
alert(countchar("BANANA","A"));
Minimum
function min(a, b){
if (a<b) return a;
else return b;
}
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);
}
document.write(isEven(50));
document.write(isEven(75));
document.write(isEven(-1));
Bean Counting
function countChar(string, char) {
let count = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == char) {
count += 1;
}
}
return count;
}
function countBs(string) {
return countChar(string, “B”);
}
document.write(countBs (“BBC”));
document.write(countChar (“KaKKerlaK”, “K”));
MINIMMUM
function min(x,y){
if (x<y) return x;
else return y;
}
var answer = min(25,50)
alert(answer)
RECURSION
function isEven(n){
if (n==0)return "even";
else if (n==1) return "odd";
else return isEven(n - 2);
}
console.log(isEven(14))
BEAN COUNTING
function countB(string)
{
let count = 0;
for(let i=0; i<string.length; i++){
if (string[i] == "B") {
count += 1
}
}
}
function countChar(string, char) {
let count=0;
for (let i = 0;i < string.length; i++) {
if(string[i] == char){
count += 1;
}
}
return count
}
function countB(string){
return countChar(string, "B");
}
This was hard. Not sure if I am getting better at coding or worse haha.
1.)
// The function min() or max() is an inbuilt function in java which returns minimum/maximum of two numbers.
function min(alpha, beta) {
if (alpha < beta) return alpha;
if (alpha = beta) return beta;
}
console.log(min(0, 10));
console.log(min(0, -10));
2.)
// Write a method isEven that uses the remainder operator (%) to determine whether an integer is even.
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));
// → false
3.)
function count_char(string, ch) {
// - starting point of counting
let counted = 0;
// - for loop -
for (let i = 0; i < string.length; i++) {
if (string[i] == ch) {
counted += 1;
}
}
return counted;
}
function count_bs(string) {
return count_char(string, "B");
}
console.log(count_bs("BBC"));
//count all "B"s in BBC (2)
console.log(count_char("kakkerlak", "k"));
//count all "k"s in kakkerlak (4)
MATH.MIN clone:
function lowNumb(a, b){
if (a <= b) {
return a;
} else {
return b;
}
}
RECURSION:
function isEven(N){
if (N == 0){
return true;
} else if (N == 1){
return false;
} else if (N < 0){
return isEven(N + 2);
} else {
return isEven(N - 2);
}
}
BEAN COUNTING:
function countBs(str1){
return countChar(str1,"B");
}
function countChar(str2,lettr){
var u = 0
for(N = 0; N < str2.length; N++){
if(str2[N] == lettr){
u++;
}
}
return u;
}