Instructions: Ask for a string and a number and remove the nth character of that string. Negative number will remove character absolute value nth from the last
<script>
function eat() {
var word = document.getElementById('word').value;
var nth = document.getElementById('nth').value;
nth = parseInt(nth);
var p1;
var p2;
// pos num
if (nth > 0) {
p1 = word.slice(0, nth - 1);
p2 = word.slice(nth, word.length)
}
else if (nth === -1) {
p1 = word.slice(0, nth);
p2 = word.slice(nth, word.length - 1);
}
// neg num
else if (nth < 0) {
p1 = word.slice(0, nth);
p2 = word.slice(nth + 1, word.length);
}
// combine string
var ans = p1 + p2;
// if nth's absolute value is bigger than the word's length
if (nth > word.length || nth < 0 - word.length) {
ans = "There's nothing there!";
}
// 0 nth
else if (nth === 0) {
ans = word;
}
// print
document.getElementById('ans').innerHTML = ans;
}
</script>
This was hard! Look at my code! I feel like there's a better way?