Skip to content

Instantly share code, notes, and snippets.

Avatar
👩‍💻
...

Happiness Nwosu NEbere

👩‍💻
...
View GitHub Profile
@NEbere
NEbere / sum.js
Created Feb 2, 2016
Calculates the sum of 1 to 1000, if the number is a multiple of 5 or three,double it and add it to the sum.
View sum.js
$(document).ready(function(){
var sum1= 0, res = 0, totalSum = 0;
for (var x = 1; x <= 1000; x++)
{
if (x % 3 === 0 || x % 5 === 0)
{
k = x * 2;
sum1 += k;
@NEbere
NEbere / factorial.php
Created Feb 11, 2016
The factorial of a given number
View factorial.php
<?php
function factorial($n) {
$factorial = 1;
for($i = $n; $i >= 1; $i--){
$factorial = $factorial * $i;
}
echo "Factorial of $n is $factorial";
}
//calling the function
@NEbere
NEbere / sum.php
Created Feb 11, 2016
Sum of the integers in a string
View sum.php
<?php
function digitSum($n) {
$array = str_split($n);
$sum = array_sum($array);
print $sum;
}
//calling the function
digitSum("123");
@NEbere
NEbere / reverse.js
Created Feb 11, 2016
Reverse a string in vanilla javascript
View reverse.js
function reverseString(str) {
return str.split("").reverse().join("");
}
alert(reverseString("hello"));
@NEbere
NEbere / palindrome.js
Created Feb 11, 2016
checks for palindromes. returns true if given string is a palindrome and false if not
View palindrome.js
function palindrome(str) {
var newStr = str.replace(/[^a-zA-Z 0-9]+| /g,'').toLowerCase();
var newStr1 = newStr.split('').reverse().join('');
if(newStr == newStr1){
return true;
}
else{
return false;
}
@NEbere
NEbere / longest_string.js
Created Feb 11, 2016
Return the length of the longest word in the provided sentence.
View longest_string.js
function findLongestWord(str) {
var strArray = str.toUpperCase().split(' ').sort();
var longest = 0;
for (var i = 0; i < strArray.length; i++) {
if (strArray[i].length > longest) {
longest = strArray[i].length;
}
}
return longest;
@NEbere
NEbere / title_case.js
Created Feb 11, 2016
Return the provided string with the first letter of each word capitalized.
View title_case.js
function titleCase(str) {
var strArray = str.toLowerCase().split(' ');
for (var i = 0; i < strArray.length; i++) {
strArray[i] = strArray[i][0].toUpperCase() + strArray[i].slice(1);
}
return strArray.join(' ');
}
titleCase("I'm a little tea pot");
@NEbere
NEbere / largest_number_inArray.js
Created Feb 11, 2016
Returns the largest number in each sub array
View largest_number_inArray.js
function largestOfFour(arr) {
var results = [];
for (var n in arr) {
var largestNumber = 0;
for (var sb in arr[n]) {
if (arr[n][sb] > largestNumber) {
largestNumber = arr[n][sb];
}
}
results[n] = largestNumber;
@NEbere
NEbere / ending.js
Created Feb 11, 2016
Check if a string (first argument) ends with the given target string (second argument).
View ending.js
function end(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
var tagLen = target.length;
var str_len = ((-str.length) + tagLen );
var sub_str = str.substr(-tagLen, tagLen);
if(target == sub_str){
return true;
@NEbere
NEbere / rotate_array.py
Last active Nov 24, 2016
Given an array of integers and a number, , perform left rotations on the array.
View rotate_array.py
# A left rotation operation on an array of size shifts each of the array's elements 1 unit to the left.
# For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2]
#Given an array of n integers and a number,d , perform d left rotations on the array.
#Then print the updated array as a single line of space-separated integers.
def rotate(array, position):
#print
print (array[position:] + array[:position])
#or return
You can’t perform that action at this time.