Earn 10 Coins ๐Ÿ’ฐfor each snippet ๐Ÿ‘‰
Share Snippet
2
profile photo
Creating and Subscribing to an observable
catherine.kimani 2022-11-14 09:15:18
                            
//creating and subscribing to an observable in angular.
//import the observable from rxjs
import { Observable }  from 'rxjs';
//export class
export class AppComponent implements OnInit {
  
//create the observable
myObservable = new Observable ((subscriber)=>{
  console.log("Observable starts")
  subscriber.next("1")
  subscriber.next("2")
  subscriber.next("3")
  subscriber.next("4")
  
});
//subscribing to the observable
ngOnInit() {
  this.myObservable.subscribe((val)=>{
    console.log(val);
  })
}
}
                            
                          
2
profile photo
C++ program to grade a student's marks using if ... else statement
Brigit Chelangat 2022-10-24 14:46:51
                            
//C++ program to determine the grade of a student
#include<iostream>
using namespace std;

int main()
{
	//variable declaration
	int marks;
 
    //prompting user to input marks
    cout<<"Enter the marks: ";
    cin>>marks;
    
    //Grading
    if(marks >= 70)
	{
		cout<<"Grade A"<<endl;
		cout<<"Excellent.";
	}
	else if(marks >= 60)
	{
		cout<<"Grade B"<<endl;
		cout<<"Good.";
	}
    else if(marks >= 50)
	{
		cout<<"Grade C"<<endl;
		cout<<"Average.";
	}
	else if(marks >= 40)
	{
		cout<<"Grade D"<<endl;
		cout<<"Fair.";
	}
	else
	{
		cout<<"Fail"<<endl;
		
	}
	return 0;
}
                            
                          
1
profile photo
SQL LIKE Operator
Ngeno Victor 2022-10-24 13:14:40
                            
SELECT * FROM Members
WHERE name LIKE 'victor%';
                            
                          
1
profile photo
SQL DISTICNT Example
Ngeno Victor 2022-10-24 13:13:23
                            
SELECT DISTINCT Communities FROM Articles;
                            
                          
1
profile photo
SQL SELECT WHERE Clause
Ngeno Victor 2022-10-24 13:12:15
                            
SELECT * FROM Articles
WHERE Community='Web';
                            
                          
1
profile photo
Python function to Add 2 numbers
Ngeno Victor 2022-10-24 12:04:26
                            
def Add(a,b):
    sum = int(a) + int(b)
    return sum
    
# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# Display the sum
print('The sum is:', (Add(num1,num2)))
                            
                          
1
profile photo
Python program to convert a list to a string by using the join() method
Ngeno Victor 2022-10-24 11:48:26
                            
myList = ['CodeAfrika', 'Developer', 'Community']

# Converting to string  
myString = ' '.join(myList)
# Display string
print(myString)
                            
                          
2
profile photo
C++ Program to find the largest number in an array
Ngeno Victor 2022-10-24 11:29:28
                            
#include <iostream>
using namespace std;

int main() {

  int i, n;
  float myArr[10];

  cout << "What is the total number of elements(1 to 10)?: ";
  cin >> n;
  cout << endl;

  // Capture each number entered by user
  for(i = 0; i < n; ++i) {
    cout << "Enter Number " << i + 1 << " : ";
    cin >> myArr[i];
  }

  // Loop through to find the largest number in the array
  for(i = 1;i < n; ++i) {
    if(myArr[0] < myArr[i])
      myArr[0] = myArr[i];
  }
  cout << endl << "The Largest number = " << myArr[0];

  return 0;
}
                            
                          
1
profile photo
Javascript - document.getElementById() method
Ngeno Victor 2022-10-24 11:11:23
                            
<script type="text/javascript">  
function getPost(){  
var postID=document.getElementById("post").value;  
alert(postID);  
}  
</script>  
<form>  
Enter Post ID:<input type="text" id="post" name="post"/><br/>  
<input type="button" value="post" onclick="getPost()"/>  
</form>
                            
                          
2
profile photo
JavasCript program to add a key/value pair to an object
Ngeno Victor 2022-10-24 11:02:00
                            
const community = {
    name: 'Web',
    members: 322,
    pos: 1
}

// add key/value pair
community.lead = "Helu Bore";

console.log(community);
                            
                          
3
profile photo
How To Use .map() to Iterate Through Array Items in JavaScript
Douglas Obara 2022-10-20 07:27:47
                            
// items in an array
const codeAfrikaCommunities = [
  "Web",
  "Mobile",
  "Cyber Security",
  "Cloud",
  "AI",
];

// iterating through the array 
const ourCommunity = codeAfrikaCommunities.map(community=>(community))

// log to the console 
console.log(ourCommunity);

// Output
[ 'Web', 'Mobile', 'Cyber Security', 'Cloud', 'AI' ]