I am trying to create the .words() method. It is supposed to split a sentence into an array of strings. It does this by identifying where there is a space character in the array and splitting up words at this point. Please note I initially did not want to use the .split() method. Why does the current code I have written not work? I do not even genuinely understand what happens when I run the code.
const _ = {
clamp(ein, lower, upper){
let zwe = Math.max(ein, lower);
let dre = Math.min(zwe, upper);
return dre;
},
inRange(ers, start, end){
if (!end){
end = start;
start = 0;
}else if (end < start){
let tmp = end;
end = start;
start = tmp;
}
(ers <= start && ers < end)? true : false;
},
words(string){
let wordArray = [];
let ers = 0;
let zwe = string.indexOf(' ', ers);
while(zwe <= string.length){
let word = string.slice(ers, zwe);
wordArray.push(word);
ers += zwe + 1;
zwe += string.indexOf(' ', ers);
}
return wordArray;
}
}
console.log(_.words('this should please work'));
// Do not write or modify code below this line.
module.exports = _;