sumAll.js

This commit is contained in:
Mohammed Nabeel 2020-07-17 10:31:47 +03:00
parent e17dc39c38
commit 11e496bcfd
50 changed files with 0 additions and 6602 deletions

View File

@ -1,25 +0,0 @@
These are a series of javascript exercises intended to be used alongside the curriculum at 'The Odin Project' They start very simply, but get more involved as you progress through them.
There will eventually be a suggested order of completion, but at this time since we are still in the process of creating more exercises the order is subject to change and has not yet been specified... In general however there are a couple which make a good "starting point" feel free to at least start with these:
1. Hello World
1. Repeat String
1. Reverse String
## HOW TO USE THESE EXERCISES
Before you start you should have a few things installed on your machine:
1. NPM. To check if you have NPM, type `npm --version` in a terminal. If you get back `Command 'npm' not found, but can be installed with:`, do NOT follow the instructions in the terminal to install with `apt-get` (this causes permission issues). Instead, install NPM/Node with NVM by following the instructions [here](https://github.com/TheOdinProject/curriculum/blob/master/web_development_101/installations/installing_node.md).
3. Jasmine. Jasmine is a testing framework for Javascript. Type `jasmine -v` to check for it. If you need to install it, type `npm install -g jasmine` to do so.
4. Clone this repo and get started.
Each exercise includes 3 files, a markdown file with a description of the task, an empty (or mostly empty) javascript file, and a set of tests. To complete the exercise go to the exercise directory with `cd helloWorld` in a terminal and run `jasmine filename.spec.js`. This should find and run the test file and show you the output. Upon first running the tests you will find that the tests fail: this is by design! Your task is to open up the javascript file and write the code needed to get all of the tests to pass. Some of the exercises have test conditions defined in the spec file that are defined as 'xit' compared to 'it'. This is purposeful, and as you test your solution against the first 'it', on success you will change the next 'xit' to an 'it' and test your code again, until all conditions are satisfied.
The first exercise, `helloWorld` will walk you through the process in more depth.
## Solutions
Solutions for these exercises can be found in this repo on the 'solutions' branch.
## a quick note!
The generator exercise is not actually an exercise… it is a script that generates exercises. I was using it when I wrote them so I didnt have to hack out the same boilerplate code every time I wrote a new one.

BIN
caesar/.DS_Store vendored

Binary file not shown.

View File

@ -1,33 +0,0 @@
# Exercise XX - caesar cipher
Implement the legendary caesar cipher:
> In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
write a function that takes a string to be encoded and a shift factor and then returns the encoded string:
```javascript
caesar('A', 1) // simply shifts the letter by 1: returns 'B'
```
the cipher should retain capitalization:
```javascript
caesar('Hey', 5) // returns 'Mjd;
```
should _not_ shift punctuation:
```javascript
caesar('Hello, World!', 5) //returns 'Mjqqt, Btwqi!'
```
the shift should wrap around the alphabet:
```javascript
caesar('Z', 1) // returns 'A'
```
negative numbers should work as well:
```javascript
caesar('Mjqqt, Btwqi!', -5) // returns 'Hello, World!'
```

View File

@ -1,5 +0,0 @@
const caesar = function() {
}
module.exports = caesar

View File

@ -1,25 +0,0 @@
const caesar = require('./caesar')
describe('caesar', function() {
it('works with single letters', function() {
expect(caesar('A', 1)).toEqual('B');
});
xit('works with words', function() {
expect(caesar('Aaa', 1)).toEqual('Bbb');
});
xit('works with phrases', function() {
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
});
xit('works with negative shift', function() {
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
});
xit('wraps', function() {
expect(caesar('Z', 1)).toEqual('A');
});
xit('works with large shift factors', function() {
expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
});
xit('works with large negative shift factors', function() {
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');
});
});

View File

@ -1,6 +0,0 @@
The goal for this exercise is to create a calculator that does the following:
add, subtract, get the sum, multiply, get the power, and find the factorial
In order to do this please fill out each function with your solution. Make sure to return the value so you can test it in Jasmine! To see the expected value
take a look at the spec file that houses the Jasmine test cases.

View File

@ -1,32 +0,0 @@
function add () {
}
function subtract () {
}
function sum () {
}
function multiply () {
}
function power() {
}
function factorial() {
}
module.exports = {
add,
subtract,
sum,
multiply,
power,
factorial
}

View File

@ -1,77 +0,0 @@
const calculator = require ('./calculator.js');
describe('add', function() {
it('adds 0 and 0', function() {
expect(calculator.add(0,0)).toEqual(0);
});
xit('adds 2 and 2', function() {
expect(calculator.add(2,2)).toEqual(4);
});
xit('adds positive numbers', function() {
expect(calculator.add(2,6)).toEqual(8);
});
});
describe('subtract', function() {
xit('subtracts numbers', function() {
expect(calculator.subtract(10,4)).toEqual(6);
});
});
describe('sum', function() {
xit('computes the sum of an empty array', function() {
expect(calculator.sum([])).toEqual(0);
});
xit('computes the sum of an array of one number', function() {
expect(calculator.sum([7])).toEqual(7);
});
xit('computes the sum of an array of two numbers', function() {
expect(calculator.sum([7,11])).toEqual(18);
});
xit('computes the sum of an array of many numbers', function() {
expect(calculator.sum([1,3,5,7,9])).toEqual(25);
});
});
describe('multiply', function() {
xit('multiplies two numbers', function() {
expect(calculator.multiply([2,4])).toEqual(8);
});
xit('multiplies several numbers', function() {
expect(calculator.multiply([2,4,6,8,10,12,14])).toEqual(645120);
});
});
describe('power', function() {
xit('raises one number to the power of another number', function() {
expect(calculator.power(4,3)).toEqual(64); // 4 to third power is 64
});
});
describe('factorial', function() {
xit('computes the factorial of 0', function() {
expect(calculator.factorial(0)).toEqual(1); // 0! = 1
});
xit('computes the factorial of 1', function() {
expect(calculator.factorial(1)).toEqual(1);
});
xit('computes the factorial of 2', function() {
expect(calculator.factorial(2)).toEqual(2);
});
xit('computes the factorial of 5', function() {
expect(calculator.factorial(5)).toEqual(120);
});
xit('computes the factorial of 10', function() {
expect(calculator.factorial(10)).toEqual(3628800);
});
});

View File

@ -1,10 +0,0 @@
# Exercise XX - fibonacci
Create a function that returns a specific member of the fibonacci sequence:
> a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.
```javascript
fibonacci(4) // returns the 4th member of the series: 3 (1, 1, 2, 3)
fibonacci(6) // returns 8
```

View File

@ -1,5 +0,0 @@
const fibonacci = function() {
}
module.exports = fibonacci

View File

@ -1,31 +0,0 @@
const fibonacci = require('./fibonacci')
describe('fibonacci', function() {
it('works', function() {
expect(fibonacci(4)).toEqual(3);
});
xit('works', function() {
expect(fibonacci(6)).toEqual(8);
});
xit('works', function() {
expect(fibonacci(10)).toEqual(55);
});
xit('works', function() {
expect(fibonacci(15)).toEqual(610);
});
xit('works', function() {
expect(fibonacci(25)).toEqual(75025);
});
xit('doesn\'t accept negatives', function() {
expect(fibonacci(-25)).toEqual("OOPS");
});
xit('DOES accept strings', function() {
expect(fibonacci("1")).toEqual(1);
});
xit('DOES accept strings', function() {
expect(fibonacci("2")).toEqual(1);
});
xit('DOES accept strings', function() {
expect(fibonacci("8")).toEqual(21);
});
});

View File

@ -1,9 +0,0 @@
# Find the Oldest
given an array of objects representing people with a birth and death year, return the oldest person.
## Hints
- You should return the whole person object, but the tests mostly just check to make sure the name is correct.
- this can be done with a couple of chained array methods, or by using `reduce`.
- One of the tests checks for people with no death-date.. use JavaScript's Date function to get their age as of today.

View File

@ -1,5 +0,0 @@
let findTheOldest = function() {
}
module.exports = findTheOldest

View File

@ -1,63 +0,0 @@
let findTheOldest = require('./findTheOldest')
describe('findTheOldest', function() {
it('finds the oldest person!', function() {
const people = [
{
name: 'Carly',
yearOfBirth: 1942,
yearOfDeath: 1970,
},
{
name: 'Ray',
yearOfBirth: 1962,
yearOfDeath: 2011
},
{
name: 'Jane',
yearOfBirth: 1912,
yearOfDeath: 1941
},
]
expect(findTheOldest(people).name).toEqual('Ray');
});
xit('finds the oldest person if someone is still living', function() {
const people = [
{
name: 'Carly',
yearOfBirth: 2018,
},
{
name: 'Ray',
yearOfBirth: 1962,
yearOfDeath: 2011
},
{
name: 'Jane',
yearOfBirth: 1912,
yearOfDeath: 1941
},
]
expect(findTheOldest(people).name).toEqual('Ray');
});
xit('finds the oldest person if the OLDEST is still living', function() {
const people = [
{
name: 'Carly',
yearOfBirth: 1066,
},
{
name: 'Ray',
yearOfBirth: 1962,
yearOfDeath: 2011
},
{
name: 'Jane',
yearOfBirth: 1912,
yearOfDeath: 1941
},
]
expect(findTheOldest(people).name).toEqual('Carly');
});
});

View File

@ -1,11 +0,0 @@
root = true
[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

View File

@ -1,2 +0,0 @@
coverage
**/templates

View File

@ -1 +0,0 @@
* text=auto

View File

@ -1,2 +0,0 @@
node_modules
coverage

View File

@ -1,5 +0,0 @@
language: node_js
node_js:
- 7
- 6
- 4

View File

@ -1,9 +0,0 @@
{
"generator-node": {
"promptValues": {
"authorName": "Cody Loyd",
"authorEmail": "codyloyd@gmail.com",
"authorUrl": "codyloyd.com"
}
}
}

View File

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2017 Cody Loyd <codyloyd@gmail.com> (codyloyd.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -1,36 +0,0 @@
# generator-exercise [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url]
> generates boilerplate for The Odin Project exercises
## Installation
First, install [Yeoman](http://yeoman.io) and generator-exercise using [npm](https://www.npmjs.com/) (we assume you have pre-installed [node.js](https://nodejs.org/)).
```bash
npm install -g yo
npm install -g generator-exercise
```
Then generate your new project:
```bash
yo exercise
```
## Getting To Know Yeoman
* Yeoman has a heart of gold.
* Yeoman is a person with feelings and opinions, but is very easy to work with.
* Yeoman can be too opinionated at times but is easily convinced not to be.
* Feel free to [learn more about Yeoman](http://yeoman.io/).
## License
MIT © [Cody Loyd](codyloyd.com)
[npm-image]: https://badge.fury.io/js/generator-exercise.svg
[npm-url]: https://npmjs.org/package/generator-exercise
[travis-image]: https://travis-ci.org/codyloyd/generator-exercise.svg?branch=master
[travis-url]: https://travis-ci.org/codyloyd/generator-exercise
[daviddm-image]: https://david-dm.org/codyloyd/generator-exercise.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/codyloyd/generator-exercise

View File

@ -1,41 +0,0 @@
'use strict';
const Generator = require('yeoman-generator');
const chalk = require('chalk');
const yosay = require('yosay');
module.exports = class extends Generator {
prompting() {
// Have Yeoman greet the user.
this.log(chalk.red('Let\'s do this'));
const prompts = [{
type: 'input',
name: 'title',
message: 'Enter the exercise title',
default: 'title'
}];
return this.prompt(prompts).then(props => {
// To access props later use this.props.someAnswer;
this.props = props;
});
}
writing() {
this.fs.copyTpl(
this.templatePath(`title.js`),
this.destinationPath(`${this.props.title}.js`),
{title: this.props.title}
);
this.fs.copyTpl(
this.templatePath(`title.spec.js`),
this.destinationPath(`${this.props.title}.spec.js`),
{title: this.props.title}
);
this.fs.copyTpl(
this.templatePath(`README.md`),
this.destinationPath(`README.md`),
{title: this.props.title}
);
}
};

View File

@ -1,2 +0,0 @@
# Exercise XX - <%= title %>

View File

@ -1 +0,0 @@
<%= title %>

View File

@ -1,5 +0,0 @@
let <%= title %> = function() {
}
module.exports = <%= title %>

View File

@ -1,8 +0,0 @@
let <%= title %> = require('./<%=title%>')
describe('<%=title%>', function() {
it('EDITME', function() {
expect(<%=title%>()).toEqual(' ');
});
});

File diff suppressed because it is too large Load Diff

View File

@ -1,51 +0,0 @@
{
"name": "generator-exercise",
"version": "0.0.0",
"description": "generates boilerplate for The Odin Project exercises",
"homepage": "theodinproject.com",
"author": {
"name": "Cody Loyd",
"email": "codyloyd@gmail.com",
"url": "codyloyd.com"
},
"files": [
"generators"
],
"main": "generators/index.js",
"keywords": [
"lame",
"yeoman-generator"
],
"devDependencies": {
"yeoman-test": "^1.6.0",
"yeoman-assert": "^3.0.0",
"nsp": "^2.6.3",
"eslint": "^4.1.0",
"eslint-config-xo-space": "^0.16.0",
"jest": "^19.0.2",
"jest-cli": "^20.0.0"
},
"dependencies": {
"chalk": "^1.1.3",
"extend": "^3.0.2",
"yeoman-generator": "^1.0.0",
"yosay": "^2.0.0"
},
"jest": {
"testEnvironment": "node"
},
"scripts": {
"prepublish": "nsp check",
"pretest": "eslint . --fix",
"test": "jest"
},
"eslintConfig": {
"extends": "xo-space",
"env": {
"jest": true,
"node": true
}
},
"repository": "git@github.com:TheOdinProject/javascript-exercises.git",
"license": "MIT"
}

View File

@ -1,26 +0,0 @@
# Get the Titles!
You are given an array of objects that represent books with an author and a title that looks like this:
```javascript
const books = [
{
title: 'Book',
author: 'Name'
},
{
title: 'Book2',
author: 'Name2'
}
]
```
your job is to write a function that takes the array and returns an array of titles:
```javascript
getTheTitles(books) // ['Book','Book2']
```
## Hints
- You should use a built-in javascript method to do most of the work for you!

View File

@ -1,5 +0,0 @@
const getTheTitles = function() {
}
module.exports = getTheTitles;

View File

@ -1,19 +0,0 @@
let getTheTitles = require('./getTheTitles')
describe('getTheTitles', function() {
const books = [
{
title: 'Book',
author: 'Name'
},
{
title: 'Book2',
author: 'Name2'
}
]
it('gets titles', function() {
expect(getTheTitles(books)).toEqual(['Book','Book2']);
});
});

View File

@ -1,16 +0,0 @@
# Exercise XX - leapYears
Create a function that determines whether or not a given year is a leap year. Leap years are determined by the following rules:
> Leap years are years divisible by four (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are divisible by 400 (like 1600 and 2000, which were in fact leap years). (Yes, it's all pretty confusing, but not as confusing as having July in the middle of the winter, which is what would eventually happen.)
>
> -- <cite>[Learn to Program](https://pine.fm/LearnToProgram/chap_06.html) by Chris Pine</cite>
```javascript
leapYears(2000) // is a leap year: returns true
leapYears(1985) // is not a leap year: returns false
```
## Hints
- use an `if` statement and `&&` to make sure all the conditions are met properly

View File

@ -1,5 +0,0 @@
const leapYears = function() {
}
module.exports = leapYears

View File

@ -1,22 +0,0 @@
const leapYears = require('./leapYears')
describe('leapYears', function() {
it('works with non century years', function() {
expect(leapYears(1996)).toEqual(true);
});
xit('works with non century years', function() {
expect(leapYears(1997)).toEqual(false);
});
xit('works with ridiculously futuristic non century years', function() {
expect(leapYears(34992)).toEqual(true);
});
xit('works with century years', function() {
expect(leapYears(1900)).toEqual(false);
});
xit('works with century years', function() {
expect(leapYears(1600)).toEqual(true);
});
xit('works with century years', function() {
expect(leapYears(700)).toEqual(false);
});
});

View File

@ -1,19 +0,0 @@
# Exercise XX - palindromes
Write a function that determines whether or not a given string is a palindrome.
A palindrome is a string that is spelled the same both forwards and backwards, usually without considering punctuation or word breaks:
### some palindromes:
- A car, a man, a maraca.
- Rats live on no evil star.
- Lid off a daffodil.
- Animal loots foliated detail of stool lamina.
- A nut for a jar of tuna.
```javascript
palindromes('racecar') // true
palindromes('tacos') // false
```

View File

@ -1,5 +0,0 @@
const palindromes = function() {
}
module.exports = palindromes

View File

@ -1,20 +0,0 @@
const palindromes = require('./palindromes')
describe('palindromes', function() {
it('works with single words', function() {
expect(palindromes('racecar')).toEqual(true);
});
xit('works with punctuation', function() {
expect(palindromes('Racecar!')).toEqual(true);
});
xit('works with multiple words', function() {
expect(palindromes('A car, a man, a maraca.')).toEqual(true);
});
xit('works with multiple words', function() {
expect(palindromes('Animal loots foliated detail of stool lamina.')).toEqual(true);
});
xit('doesn\'t just always return true', function() {
expect(palindromes('ZZZZ car, a man, a maraca.')).toEqual(false);
});
});

View File

@ -1,10 +0,0 @@
This exercise is tricky and was removed from our recommendations because it mostly leverages regular expressions for the solution, and those aren't really taught at this point in our curriculum.
Leaving it here for posterity, or a good challenge for anyone that wants to give it a shot.
Pig Latin is a children's language that is intended to be confusing when spoken quickly. Your job for this exercise is to create a solution that takes the words given and
turns them into pig latin. Please see the following wikipedia page for details regarding the rules of Pig Latin:
https://en.wikipedia.org/wiki/Pig_Latin
The rules section will give the rules and the examples that are required to complete this exercise.

View File

@ -1,9 +0,0 @@
function translate() {
// body...
}
module.exports = {
translate
}

View File

@ -1,64 +0,0 @@
// Topics
// * modules
// * strings
// Pig Latin
// Pig Latin is a made-up children's language that's intended to be confusing. It obeys a few simple rules (below) but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand.
// Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
// Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word.
// (There are a few more rules for edge cases, and there are regional variants too, but that should be enough to understand the tests.)
// See https://en.wikipedia.org/wiki/Pig_Latin for more details.
const pigLatin = require("./pigLatin.js");
describe('#translate', function() {
it('translates a word beginning with a vowel', function() {
s = pigLatin.translate("apple");
expect(s).toEqual('appleay');
});
xit('translates a word beginning with a consonant', function() {
s = pigLatin.translate("banana");
expect(s).toEqual("ananabay");
});
xit('translates a word beginning with two consonants', function() {
s = pigLatin.translate("cherry");
expect(s).toEqual('errychay');
});
xit('translates two words', function() {
s = pigLatin.translate("eat pie");
expect(s).toEqual('eatay iepay');
});
xit('translates a word beginning with three consonants', function() {
expect(pigLatin.translate("three")).toEqual("eethray");
});
xit('counts "sch" as a single phoneme', function() {
s = pigLatin.translate("school");
expect(s).toEqual("oolschay");
});
xit('counts "qu" as a single phoneme', function() {
s = pigLatin.translate("quiet");
expect(s).toEqual("ietquay");
});
xit('counts "qu" as a consonant even when its preceded by a consonant', function() {
s = pigLatin.translate("square");
expect(s).toEqual("aresquay");
});
xit('translates many words', function() {
s = pigLatin.translate("the quick brown fox");
expect(s).toEqual("ethay ickquay ownbray oxfay");
});
});

View File

@ -1,17 +0,0 @@
# Exercise 04 - removeFromArray
Implement a function that takes an array and some other arguments then removes the other arguments from that array:
```javascript
removeFromArray([1, 2, 3, 4], 3); // should remove 3 and return [1,2,4]
```
## Hints
the first test on this one is fairly easy, but there are a few things to think about(or google) here for the later tests:
- how to remove a single element from an array
- how to deal with multiple optional arguments in a javascript function
- [Check this link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments). Scroll down to the bit about `Array.from` or the spread operator. - [Or this link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).

View File

@ -1,19 +0,0 @@
const removeFromArray = function(...args) {
let myArray = args[0]; //Create an array with the first function argument
let pos = 0; //Create a variable to store index of element
// Remove all the remaining elements from the array
for (let i = 1; i < args.length; i++) {
pos = myArray.indexOf(args[i]);
if (pos >= 0 && myArray[pos] === args[i]) { //Check argument and type
myArray.splice(pos, 1);
}
}
return myArray;
}
module.exports = removeFromArray

View File

@ -1,25 +0,0 @@
const removeFromArray = require('./removeFromArray')
describe('removeFromArray', function() {
it('removes a single value', function() {
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
});
it('removes multiple values', function() {
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
});
it('ignores non present values', function() {
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
});
it('ignores non present values, but still works', function() {
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
});
it('can remove all values', function() {
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
});
it('works with strings', function() {
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
});
it('only removes same type', function() {
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
});
});

View File

@ -1,18 +0,0 @@
# Exercise 02 - repeatString
Write a function that simply repeats the string a given number of times:
```javascript
repeatString('hey', 3) // returns 'heyheyhey'
```
You will notice in this exercise that there are multiple tests (see in file `repeatString.spec.js`). Only the first test is currently enabled. So after making sure that this first one passes, enable the others one by one by deleting the `x` in front of the `it()` function.
## Hints
- You're going to want to use a loop for this one.
- Create a variable to hold the string you're going to return, create a loop that repeats the given number of times and add the given string to the result on each loop.
- If running `jasmine repeatString.spec.js` raises `Temporarily disabled with xit` errors, make sure you have enabled the rest of the tests (see above).

View File

@ -1,17 +0,0 @@
const repeatString = function(string, number) {
let result = '';
if (number < 0) {
return 'ERROR';
}
for (let i = 0; i < number; i++) {
result += string;
}
return result;
}
module.exports = repeatString

View File

@ -1,33 +0,0 @@
const repeatString = require('./repeatString')
describe('repeatString', function() {
it('repeats the string', function() {
expect(repeatString('hey', 3)).toEqual('heyheyhey');
});
it('repeats the string many times', function() {
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
});
it('repeats the string 1 times', function() {
expect(repeatString('hey', 1)).toEqual('hey');
});
it('repeats the string 0 times', function() {
expect(repeatString('hey', 0)).toEqual('');
});
it('returns ERROR with negative numbers', function() {
expect(repeatString('hey', -1)).toEqual('ERROR');
});
it('repeats the string a random amount of times', function() {
/*The number is generated by using Math.random to get a value from between
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
equals a number between 0 to 999 (this number will change everytime you run
the test).*/
const number = Math.floor(Math.random() * 1000)
/*The .match(/((hey))/g).length is a regex that will count the number of heys
in the result, which if your function works correctly will equal the number that
was randomaly generated. */
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(number);
});
it('works with blank strings', function() {
expect(repeatString('', 10)).toEqual('');
});
});

View File

@ -1,15 +0,0 @@
# Exercise 02 - Reverse a String.
Pretty simple, write a function called `reverseString` that returns its input, reversed!
```javascript
reverseString('hello there') // returns 'ereht olleh'
```
You will notice in this exercise that there are multiple tests, after making the first one pass, enable the others one by one by deleting the `x` in front of the `it()` function.
## Hints
Strings in JavaScript cannot be reversed directly so you're going to have to split it into something else first.. do the reversal and then join it back together into a string.

View File

@ -1,14 +0,0 @@
const reverseString = function(string) {
let revString = '';
for (let i = string.length - 1; i >= 0; i--) {
revString += string.charAt(i);
}
return revString;
}
module.exports = reverseString

View File

@ -1,18 +0,0 @@
const reverseString = require('./reverseString')
describe('reverseString', function() {
it('reverses single word', function() {
expect(reverseString('hello')).toEqual('olleh');
});
it('reverses multiple words', function() {
expect(reverseString('hello there')).toEqual('ereht olleh')
})
it('works with numbers and punctuation', function() {
expect(reverseString('123! abc!')).toEqual('!cba !321')
})
it('works with blank strings', function() {
expect(reverseString('')).toEqual('')
})
});