Skip to content

cap-swap #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CAP_SWAP_BUTTON = 'swap-button';
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import { capSwap } from './utils.js';
import { capSwap } from './util.js';

export const capSwapHandler = () => {};
() => {
// read & process user input
let userInput = null;
while (userInput === null) {
userInput = prompt('enter a string to cap-swap');
}

// execute core logic

const capSwapped = capSwap(userInput);

// communicate result to user
alert(capSwapped);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
document.getElementById('swap-button').addEventListener('click', () => {

import { CAP_SWAP_BUTTON } from '../data/constants.js';
import './listener.js';

/*document.getElementById(CAP_SWAP_BUTTON).addEventListener('click', () => {
// read & process user input
let userInput = null;
while (userInput === null) {
Comment on lines +5 to 8
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove these lines once you already import './listener';

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import { capSwapHandler } from './handler.js';
document.getElementById(CAP_SWAP_BUTTON).addEventListener('click',capSwapHandler);
22 changes: 18 additions & 4 deletions 6-refactoring/exercises/prompt-alert-confirm/cap-swap/src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
/**
*
*/
export const capSwap = () => {};

export const capSwap = (userInput) => {

const capSwapped = userInput
.split('')
.map((str) => {
const charCode = str.charCodeAt(0);
if (64 < charCode && charCode < 91) {
return String.fromCharCode(charCode + 32);
} else if (96 < charCode && charCode < 123) {
return String.fromCharCode(charCode - 32);
} else {
return str;
}
})
.join('');
return capSwapped;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { capSwap } from './utils.js';
import { capSwap } from './util.js';

describe('capSwap: makes uppercase lowercase, and lowercase uppercase', () => {
it('returns an empty string for an empty string', () => {
Expand Down
4 changes: 3 additions & 1 deletion 6-refactoring/exercises/state/no-copies/data/state.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const state = _;
export const state = {
noCopies: [],
};
16 changes: 14 additions & 2 deletions 6-refactoring/exercises/state/no-copies/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import { saveNoCopies } from './utils.js';
import { saveNoCopies } from './util.js';
import { state } from '../data/state.js';

export const saveNoCopiesHandler = () => {};
export const saveNoCopiesHandler = () => {
// read & process user input
let userInput = null;
while (userInput === null) {
userInput = prompt('enter a string to save');
}
// communicate result to user
const message = saveNoCopies(userInput);
alert(message);
// log interaction
console.log(message);
};
export default saveNoCopiesHandler;
5 changes: 5 additions & 0 deletions 6-refactoring/exercises/state/no-copies/src/init.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import './listener.js';


/*
// data to refactor out of this file
const data = {
noCopies: [],
Expand All @@ -23,3 +27,4 @@ document.getElementById('no-copies-button').addEventListener('click', () => {
// log interaction
console.log(data);
});
*/
6 changes: 6 additions & 0 deletions 6-refactoring/exercises/state/no-copies/src/listener.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
import { saveNoCopiesHandler } from './handler.js';

const listener = () => {
document.getElementById('no-copies-button').
addEventListener('click',saveNoCopiesHandler);
};
export default listener;
18 changes: 14 additions & 4 deletions 6-refactoring/exercises/state/no-copies/src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
/**
*
*/
export const saveNoCopies = () => {};


export const saveNoCopies = (userInput) => {
const alreadySaved = data.noCopies.includes(userInput);
if (!alreadySaved) {
data.noCopies.push(userInput);
}
// Communicate result to user
const message = data.noCopies.join(', ');
return data.noCopies;
};

export default saveNoCopies;

2 changes: 1 addition & 1 deletion 6-refactoring/exercises/state/no-copies/src/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { saveNoCopies } from './utils.js';
import { saveNoCopies } from './util.js';

describe('saveNoCopies: ', () => {
describe('adds a new items that are not in the array', () => {
Expand Down