There are five errors in the code below. List five of them and: identify the lin
ID: 3880962 • Letter: T
Question
There are five errors in the code below. List five of them and:
identify the line by number,
state if the error is an interpreter (syntax) error or a runtime (logic) error, and
give a corrected version of the entire line.
1 var text = prompt('Enter a sentence');
2 var letter = prompt('Enter a letter to count');
3 var num = countLetters(text, letter);
4
5 document.writeIn('Found ' + num + ' ' + letter + 's');
.6
7 Function countLetters(alpha, phrase)
8 { var cnt = 0;
9 var i = 0;
10
11 while(i<=phrase.length)
12 { if(alpha == phrase[i]);
13 cnt++;
14 i++;
15 }
16 return cnt;
17 }
Explanation / Answer
line 7: funtion f is small (syntax error)
line 11:phrase.length will be replaced by alpha.length (run time error)
line 12:alpha will have index not phrase (syntax error)
line 12: no need of semocolon (run time error)
line13:end of curley braces of } (run time error)
correct code is:
var text = prompt('Enter a sentence');
var letter = prompt('Enter a letter to count');
var num = countLetters(text, letter);
document.writeln('Found ' + num + ' ' + letter + 's');
function countLetters(alpha, phrase)
{ var cnt = 0;
var i = 0;
while(i <= alpha.length)
{
if(phrase == alpha[i])
{
cnt++;
}
i++;
}
return cnt;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.