Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem 10. Word or Number 2
- // You are given an integer number n and then n new lines of text: numbers or words (see Problem 6). This time when we have a word we concatenate it with the previous words with a dash - between them. If we have a number we add it to all previous numbers.
- // The input starts by the number n (alone in a line) followed by n lines, each holding a text without a space.
- // Again all words contain no digits.
- // The output is like in the examples below.
- // On the first line there are all words concatenated with - between them or no words if there were no words in the input.
- // On the second line is the sum of all the numbers or 0 if there were no numbers.
- // Examples:
- // input output
- // 5
- // 1
- // go
- // 1
- // there
- // 5 go-there
- // 7
- //NO text input
- let input = [
- '3',
- '1',
- '1',
- '7',
- ];
- //with text input
- // let input = [
- // '5',
- // '1',
- // 'go',
- // '1',
- // 'there',
- // '5',
- // ];
- //no numbers input
- // let input = [
- // '3',
- // 'try',
- // 'google',
- // 'it'
- // ];
- let print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- let length = +gets();
- let char = '';
- let text = '';
- let num = 0;
- for (let i = 0; i < length; i++) {
- char = gets();
- if (!isNaN(char)) {
- num += +char;
- } else if (isNaN(char)) {
- text += char + '-';
- }
- }
- if (text === '') {
- console.log('no words');
- }
- console.log(text.slice(0, -1));
- console.log(num);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement