Skip to content
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 REPLACE_STRING = 'replacing';
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
import { replaceWith } from './utils.js';
import { replaceWith } from './util.js';

export const replaceWithHandler = () => {};
export const replaceWithHandler = () => {
() => {
debugger;
// read & process user input
const stringToReplacify = prompt('enter a string to replaceify');
if (stringToReplacify === null) {
alert('good bye');
return;
}

const oldChar = prompt('enter a character to replace');
if (oldChar === null) {
alert('good bye');
return;
}
if (oldChar.length !== 1) {
alert(`"${oldChar}" is not a single character`);
return;
}

const newChar = prompt(`enter a character to replace "${oldChar}"`);
if (newChar === null) {
alert('good bye');
return;
}
if (newChar.length !== 1) {
alert(`"${newChar}" is not a single character`);
return;
}

const newString = replaceWith(stringToReplacify, oldChar, newChar);

// communicate result to user
alert(`${stringToReplacify}\n${newString}`);
}
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
document.getElementById('replacing').addEventListener('click', () => {

import './listener.js'

/*document.getElementById(REPLACE_STRING).addEventListener('click', () => {
debugger;
// read & process user input
const stringToReplacify = prompt('enter a string to replaceify');
Expand Down Expand Up @@ -28,6 +31,7 @@ document.getElementById('replacing').addEventListener('click', () => {
}

// execute core logic

let newString = '';
for (const char of stringToReplacify) {
if (char === oldChar) {
Expand All @@ -39,4 +43,4 @@ document.getElementById('replacing').addEventListener('click', () => {

// communicate result to user
alert(`${stringToReplacify}\n${newString}`);
});
});*/
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
import { REPLACE_STRING } from '../data/constants.js';
import { replaceWithHandler } from './handler.js';


document.getElementById(REPLACE_STRING).addEventListener('click', replaceWithHandler);
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
/**
*
*/
export const replaceWith = () => {};
export const replaceWith = (stringToReplacify, oldChar, newChar) => {
let newString = '';
for (const char of stringToReplacify) {
if (char === oldChar) {
newString += newChar;
} else {
newString += char;
}
}
return newString;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { replaceWith } from './utils.js';
import { replaceWith } from './util.js';

describe('replaceWith: replaces characters', () => {
it('replaces nothing in an empty string', () => {
Expand Down