0

enter image description here

So here my 3 errors. The first error, at beginning is just, that i dont named the first 2 variables.

i need to program a code for university.

We need to program an prompt box, where you can write a sentence in the entry bar, and after you pressed, "ok", it gives you the sentence in reverse letters back. We program in TypeScript. I thought JavaScript is similar.

We need to use a function, string and alert.

I tried the following one, but eclipse gives me errors, and I don't know how to manage, that it can use a variable sentence for reverseLetters. Please help me, i need to have this tomorrow.

var input: string = prompt("Place a sentence here");
var letters: string = reverseLetters (input);
alert(words + "\n" + sentence + "\n" + letters);

function reverseLetters(input){
var words = input.split(" ");
var output = new Array();
words.forEach(function(word) {
    output.push(word.split("").reverse().join(""));
});
return output.join(" ");
}
(document).ready(function () {
var sentence = "Variable Sentence";
console.log("Original Sentence: " + sentence);
var revSentence = reverseLetters(sentence);
console.log("Reverse Sentence: " + revSentence);
});
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
ts1995
  • 1
  • 2

2 Answers2

0

I can help you a bit with your current program but it's your homework:

// The variable was not defined.
var words = "TODO"; // the type is inferred by TypeScript, you don't need to write: var words: string = "TODO";
var input: string = prompt("Place a sentence here");
var letters: string = reverseLetters(input);
alert(words + "\n" + input + "\n" + letters);

// Function with a one string parameter that returns a string.
function reverseLetters(input:string):string {
    var words = input.split(" ");
    var output = new Array();
    words.forEach(function(word:string) {
        output.push(word.split("").reverse().join(""));
    });
    return output.join(" ");
}

// "Programmers can use ambient declarations to tell the TypeScript compiler that some other component will supply a variable."
// https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#11-ambient-declarations
// But it's better to use `tsd` and special comment `///<reference path="path/to/jquery.d.ts">`
declare var $;

$(document).ready(function () {
    var sentence = "Variable Sentence";
    console.log("Original Sentence: " + sentence);
    var revSentence = reverseLetters(sentence);
    console.log("Reverse Sentence: " + revSentence);
});
MartyIX
  • 27,828
  • 29
  • 136
  • 207
0

You're missing some variables. Add the following before the alert and it should be okay.

var words: string = "none";
var sentence: string = "none";
wolfhammer
  • 2,641
  • 1
  • 12
  • 11