The browser storage localStorage is not available. Either your browser does not support it or you have disabled it or the maximum memory size is exceeded. Without localStorage your solutions will not be stored.
String: replace()
The
replace
method replaces a substring with another:
let str = 'JavaScript';
let newstr = str.replace('Java', 'ECMA');
'Java'
is replaced with 'ECMA'
. Thus newstr
has the value 'ECMAScript'
.
The original string remains unchanged. Only the first occurrence is replaced:
let newstr = 'Dada'.replace('a', 'i');
newstr
has the value 'Dida'
and not 'Didi'
.Exercise
Write a function
Example:
normalize
, that replaces '-'
with '/'
in a date string.Example:
normalize('20-05-2017')
should return '20/05/2017'
.
+ Hint
function normalize(date) {
// Use replace() twice.
}
+ Solution
function normalize(date) {
let newDate = date.replace('-','/');
newDate = newDate.replace('-','/');
return newDate;
}