Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solutions #4

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions array-and-strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ Input: [1, 4, ‘i am a string’, ‘456’]
Output: “Numbers: 2, Strings: 2”

Solution:

function printNum(array) {
laramalkhasyan marked this conversation as resolved.
Show resolved Hide resolved
let numOfString = 0;
let numOfNumber = 0;
array.forEach(element => {
if (typeof element != "number")
laramalkhasyan marked this conversation as resolved.
Show resolved Hide resolved
numOfString++
else
numOfNumber++
});
console.log("Number:" + numOfNumber, "String:" + numOfString)
}



Expand All @@ -24,6 +34,15 @@ Output: "monster"

Solution:

function LongestWord(string) {
laramalkhasyan marked this conversation as resolved.
Show resolved Hide resolved
let arr = string.split(/[ ,.-]/);
let longest = "";
arr.forEach(element => {
if (element.length >= longest.length)
longest = element;
});
return longest;
}



Expand All @@ -38,7 +57,9 @@ Output: "[]"

Solution:


function Comparison(array,num){
return array.filter(x => x > num)
}


4) Write a function, which will receive a number between 0 to 999 and spell out that number in English.
Expand All @@ -51,12 +72,53 @@ Output: “nine thousand four hundred twenty five”

Solution:


function NumToText(num) {
const nums = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
const nums2 = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
const nums3 = ["twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"];


if (Math.floor(num / 10) != 0) {
let mat1 = num % 10;
if (Math.floor(num / 100) != 0) {
num = Math.floor(num / 10);
let mat2 = num % 10;
let mat3 = Math.floor(num / 10);
if(mat2 == 0 && mat1 == 0)
return nums[mat3] + " hundred ";
if (mat2 == 0) {
return nums[mat3] + " hundred " + nums[mat1];
}
if (mat2 == 1) {
return nums[mat3] + " hundred " + nums2[mat1];
}
if(mat1 == 0)
return nums[mat3] + " hundred " + nums3[mat2 - 2];
return nums[mat3] + " hundred " + nums3[mat2 - 2] + " " + nums[mat1];
}
let mat4 = Math.floor(num / 10);
if (mat4 == 1)
return nums2[mat1]
else if (mat1 == 0)
return nums3[mat4 - 2]
else
return nums3[mat4 - 2] + " " + nums[mat1]

}
return nums[num];
}


5) A left rotation operation on an array shifts each of the array's elements unit to the left. For example,
if left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 1, 2].

Solution:


function rotateWithNum (array, num){
for (let i = 0; i < num; i++) {
let temp = array[0];
array.shift();
array.push(temp);
}
return array;
}