solved 1-12
This commit is contained in:
parent
8746ce056a
commit
f48e5d15cd
|
@ -1,5 +1,5 @@
|
|||
const helloWorld = function() {
|
||||
return ''
|
||||
return 'Hello, World!'
|
||||
};
|
||||
|
||||
module.exports = helloWorld;
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
const repeatString = function() {
|
||||
|
||||
const repeatString = function(str,n) {
|
||||
if(n<0)
|
||||
return "ERROR"
|
||||
let s="";
|
||||
while(n)
|
||||
{
|
||||
s+=str;
|
||||
n--;
|
||||
}
|
||||
return s;
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const reverseString = function() {
|
||||
|
||||
const reverseString = function(str) {
|
||||
return str.split("").reverse().join("");
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
const removeFromArray = function() {
|
||||
|
||||
const removeFromArray = function(arr,...args) {
|
||||
for(let arg of args)
|
||||
{
|
||||
let index=arr.indexOf(arg);
|
||||
if(index>=0)
|
||||
{
|
||||
arr.splice(index,1);
|
||||
}
|
||||
}
|
||||
return arr
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,4 +1,25 @@
|
|||
const sumAll = function() {
|
||||
const sumAll = function(s,e) {
|
||||
if(typeof(s)==="number" && typeof(e)=="number" && s>=0 && e>=0)
|
||||
{
|
||||
let sum=0;
|
||||
if(s<=e)
|
||||
{
|
||||
for(let i=s;i<=e;i++)
|
||||
{
|
||||
sum+=i;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(let i=s;i>=e;i--)
|
||||
{
|
||||
sum+=i;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
else
|
||||
return "ERROR"
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
const leapYears = function() {
|
||||
|
||||
const leapYears = function(x) {
|
||||
if ((x%4==0 && x%100) || (x%4==0 && x%100==0 && x%40==0))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
const convertToCelsius = function() {
|
||||
const convertToCelsius = function(x) {
|
||||
let c=((x-32)*5)/9;
|
||||
return Math.round(c*10)/10;
|
||||
};
|
||||
|
||||
const convertToFahrenheit = function() {
|
||||
const convertToFahrenheit = function(x) {
|
||||
let f=((x*9)/5)+32;
|
||||
return Math.round(f*10)/10;
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,25 +1,41 @@
|
|||
const add = function() {
|
||||
|
||||
const add = function(x,y) {
|
||||
return x+y
|
||||
};
|
||||
|
||||
const subtract = function() {
|
||||
|
||||
const subtract = function(x,y) {
|
||||
return x-y
|
||||
};
|
||||
|
||||
const sum = function() {
|
||||
|
||||
const sum = function(a) {
|
||||
let s=parseInt(0);
|
||||
for(let i=0;i<a.length;i++)
|
||||
{
|
||||
s+=parseInt(a[i]);
|
||||
}
|
||||
return s;
|
||||
};
|
||||
|
||||
const multiply = function() {
|
||||
|
||||
const multiply = function(a) {
|
||||
let ans=1;
|
||||
for(let i=0;i<a.length;i++)
|
||||
{
|
||||
ans*=a[i];
|
||||
}
|
||||
return ans;
|
||||
};
|
||||
|
||||
const power = function() {
|
||||
|
||||
const power = function(x,y) {
|
||||
return Math.pow(x,y)
|
||||
};
|
||||
|
||||
const factorial = function() {
|
||||
|
||||
const factorial = function(x) {
|
||||
let f=1;
|
||||
while(x)
|
||||
{
|
||||
f*=x;
|
||||
x--;
|
||||
}
|
||||
return f;
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,5 +1,24 @@
|
|||
const palindromes = function () {
|
||||
|
||||
function checkIfLetter(s)
|
||||
{
|
||||
return s.toLowerCase() != s.toUpperCase();
|
||||
}
|
||||
const palindromes = function (str) {
|
||||
let s=0,e=str.length-1;
|
||||
str=str.toLowerCase()
|
||||
while(s<=e)
|
||||
{
|
||||
while(!checkIfLetter(str[s]))
|
||||
{
|
||||
s++;
|
||||
}
|
||||
while(!checkIfLetter(str[e]))
|
||||
e--;
|
||||
if(str[s]!=str[e])
|
||||
return false;
|
||||
s++;
|
||||
e--;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,5 +1,15 @@
|
|||
const fibonacci = function() {
|
||||
|
||||
const fibonacci = function(n) {
|
||||
if((n)<0)
|
||||
return "OOPS";
|
||||
let a=0,b=1,c;
|
||||
while((n))
|
||||
{
|
||||
c=a+b;
|
||||
a=b;
|
||||
b=c;
|
||||
n--;
|
||||
}
|
||||
return a;
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const getTheTitles = function() {
|
||||
|
||||
const getTheTitles = function(obj) {
|
||||
return obj.map((x)=>x.title)
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
const findTheOldest = function() {
|
||||
|
||||
const findTheOldest = function(obj) {
|
||||
return obj.reduce((acc,curr)=>{
|
||||
let birthCurr=curr.yearOfBirth
|
||||
let deathCurr=("yearOfDeath" in curr)?curr.yearOfDeath:(new Date().getFullYear());
|
||||
let diffCurr=(deathCurr-birthCurr)
|
||||
let birthAcc=acc.yearOfBirth
|
||||
let deathAcc=("yearOfDeath" in acc)?acc.yearOfDeath:(new Date().getFullYear());
|
||||
let diffAcc=(deathAcc-birthAcc)
|
||||
acc=(diffCurr>diffAcc)?curr:acc;
|
||||
return acc;
|
||||
})
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
|
|
Loading…
Reference in New Issue